2011-07-14 202 views
0

我需要將格式爲HHMM的字符串(在EST或EDT - 今天)轉換爲格式爲YYYYMMDD-HH:MM:SS的GMT時間戳。所以如果我得到1512,我想用格林尼治標準時間和格式表示3:12EDT:20110714-20:12:00。我怎樣才能做到這一點?將HHMM字符串轉換爲Java格式的GMT格式

我試過下面的代碼,它根本不轉換。

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(); 
+0

只需要注意一點,您不應該使用date.setHours,因爲它們已被棄用嘗試使用Calendar對象。 – RMT

+1

'gmt.format(day)'正在返回一個字符串 - 你不想要這些結果? –

+0

與日曆,你可以只cal.set(Calendar.HOUR,hourValue); – RMT

回答

0

感謝您的回覆,我結合了幾個答案,最終得到了正確轉換爲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()); 
} 
0

怎麼樣,

//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); 
+0

此代碼輸出: dt = Thu Jan 01 02:12:00 EST 1970 fmtDt = 19700101-07:12:00 – Sarah

+0

時間3:12EDT總是在當天嗎? – aldrin

+0

是的,總是今天 – Sarah

0

你確實應該使用日曆,但你有它成立現在的方式這應該工作:

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

這是一個有點短,更清晰(IMO)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)); 
} 
+0

人們不妨和Joda一起冒險! JSR-310將不會包含在JDK7中,並且在準備就緒之前,Joda肯定比內置類更好。 – jtoberon

相關問題