简体   繁体   English

在 Java 中将 HHMM 字符串转换为 GMT 格式

[英]Convert HHMM String to GMT format in Java

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.我需要将 HHMM 形式的字符串(在 EST 或 EDT - 今天)转换为 YYYYMMDD-HH:MM:SS 形式的 GMT 时间戳。 So if I get 1512, I want to express 3:12EDT in the GMT time and format: 20110714-20:12:00.因此,如果我得到 1512,我想以 GMT 时间和格式表示 3:12EDT:20110714-20:12:00。 How can 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 :Joda中它更短更清晰(IMO):

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));
}

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);

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);

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.感谢您的回复,我结合了一些答案,最终得到了以下代码,该代码正确转换为 GMT 并为我提供了我需要的格式化字符串。

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM