开发者

Convert HHMM String to GMT format in Java

开发者 https://www.devze.com 2023-03-20 03:33 出处:网络
I need to convert a String of the form HHMM (in EST or EDT - today) into a GMT timestamp of the form YYYYMMDD-HH:MM:SS.So if I get 1512, I want to express 3:12EDT in the GMT time and format: 20110714-

I need to convert a String of the form HHMM (in EST or EDT - today) into a GMT timestamp of the form YYYYMMDD-HH:MM:SS. So if I get 1512, I want to express 3:12EDT in the GMT time and format: 20110714-20:12:00. How can开发者_StackOverflow中文版 I do this?

I have tried the following code and it does not convert at all.

    int hour = Integer.parseInt(HHMM.substring(0, 2));
    int min  = Integer.parseInt(HHMM.substring(2, 4));

    Date day = new Date();
    day.setHours(hour);
    day.setMinutes(min);
    day.setSeconds(00); 
    DateFormat gmtFormat = new SimpleDateFormat();
    gmtFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

    gmtFormat.format(day);

    return day.getYear() + "" + day.getMonth() + "" + 
           day.getDate() + "-" + day.getHours() + ":" + 
           day.getMinutes() + ":" + day.getSeconds();


It's a bit shorter and clearer (IMO) in Joda:

public static String printIsoTime(String hhmm)    {
    DateTimeFormatter fmt = DateTimeFormat.forPattern("HHmm");

    // construct datetime from local midnight + parsed hhmm
    DateTime localDateTime = new LocalDate().toDateTime(
        fmt.parseDateTime(hhmm).toLocalTime());

    // convert to UTC and print in ISO format
    return ISODateTimeFormat.dateHourMinuteSecond()
        .print(localDateTime.withZone(DateTimeZone.UTC));
}


You should really be using Calendar, but the way you have it set up now this should work:

Date day = new Date();
day.setHours(3);
day.setMinutes(12);
day.setSeconds(00);

SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyyMMdd-hh:mm:ss");
gmtFormat.setTimeZone(TimeZone.getTimeZone("GMT") );

Date fmtDay = gmtFormat.format(day);
System.out.println("fmtDay: " + fmtDay);


How about,

    //obtain a formatted string with the year day month information for today
    Date today = new Date();
    SimpleDateFormat todaySdf = new SimpleDateFormat("yyyyMMdd-");
    String todaySdfString = todaySdf.format(today);

    //now concatenate today's info from above with the time info and then parse the whole thing
    SimpleDateFormat edtFormat = new SimpleDateFormat("yyyyMMdd-HH:mmz");
    Date dt = edtFormat.parse(todaySdfString+"15:12EDT");

    //now GMT
    SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyyMMdd-HH:mm:ss");
    gmtFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    String fmtDt = gmtFormat.format(dt);
    System.out.println("fmtDt = " + fmtDt);


Thank you for your responses, I combined a few answers to end up with the following code which correctly converts to GMT and gives me the formatted String I require.

private static String convertHHMMtoGMT(String HHMM)
{       
    Calendar day = Calendar.getInstance();
    day.set(Calendar.HOUR_OF_DAY, hour);
    day.set(Calendar.MINUTE, min);
    day.set(Calendar.SECOND, 00);
    day.set(Calendar.MILLISECOND, 00);    

    SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyyMMdd-HH:mm:ss");
    gmtFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

    return gmtFormat.format(day.getTime());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号