簡體   English   中英

Java將Time對象(GMT)轉​​換為String(GMT + 1)

[英]Java convert Time object (GMT) to String (GMT+1)

我嘗試了下面的代碼,它應該允許我將時間傳遞作為參數轉換為一個小時以上的字符串(GMT + 1)。
但是我總是在我的對象Time中得到同樣的時間

public static String getFormattedTimeLabel(final Time time) {
    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(time.getTime());
    SimpleDateFormat sdf = new SimpleDateFormat("HH : mm");
    sdf.setCalendar(cal);
    sdf.setTimeZone(TimeZone.getDefault());
    return sdf.format(cal.getTime());
}    

有人知道怎么解決嗎?

編輯:好的,最后我在我的控制器中創建了以下功能。
我在簡單日期格式中設置的時區是客戶端恢復的時區,這要歸功於HttpServletRequest。
您可以在代碼后看到System.out的打印結果。

private Time getTimeLocal(Time requestTime) {
     Calendar cal = new GregorianCalendar();
     cal.setTimeInMillis(requestTime.getTime());
     SimpleDateFormat sdf = new SimpleDateFormat("HH : mm");
     sdf.setCalendar(cal);
     System.out.println(requestTime);
     sdf.setTimeZone(tz);
     System.out.println(tz.getID());
     String dt = sdf.format(cal.getTime());
     Date date = null;
    try {
        date = sdf.parse(dt);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    System.out.println(dt);
     return new Time(date.getTime());
}

10:00:00 //the Time object
Europe/Paris  // The id of the client timezone (GMT+1 for me)
10 : 00 //the date. the time printed is not correct it should be set with the timezone of the client so for me GMT+1 so 11:00:00

代碼似乎正常工作。 輸出結果時,時間將顯示為正確時區中的時間。 嘗試添加時區參數,如下所示:

public static String getFormattedTimeLabel(final Time time, String tzId) {
  Calendar cal = new GregorianCalendar();
  cal.setTimeInMillis(time.getTime());
  SimpleDateFormat sdf = new SimpleDateFormat("HH : mm");
  sdf.setCalendar(cal);
  sdf.setTimeZone(tzId == null ? TimeZone.getDefault() : TimeZone.getTimeZone(tzId));
  return sdf.format(cal.getTime());
}

並將其調用為:

Time time = new Time(System.currentTimeMillis());
System.out.println(getFormattedTimeLabel(time, null));
System.out.println(getFormattedTimeLabel(time, "UTC"));

給出以下結果(截至目前):

12 : 08
11 : 08

因為我在TZ GMT + 1中,現在它已經過了中午,結果是正確的。

順便說一句,打印時間對象如下:

System.out.println(time.toString());

得到:

12:08:16

即在默認時區默認格式化。

順便說一下,該函數可以簡化為不使用Calendar對象,如下所示:

public static String getFormattedTimeLabel(final Time time, String tzId) {
  SimpleDateFormat sdf = new SimpleDateFormat("HH : mm");
  sdf.setTimeZone(tzId == null ? TimeZone.getDefault() : TimeZone.getTimeZone(tzId));
  return sdf.format(time);
}

在SimpleDateFormat中設置時區就足夠了。

好吧,我終於找到了解決方案,感謝這篇帖子在Java中的Date TimeZone轉換? 並且所有人的幫助都參與了我的職位。

我的最終代碼是:

private Time getTimeLocal(Time requestTime) {
    String ret = Utils.getFormattedTimeLabel(requestTime);
    SimpleDateFormat sdfgmt = new SimpleDateFormat("HH : mm");
    sdfgmt.setTimeZone(TimeZone.getTimeZone("GMT"));

    SimpleDateFormat sdfmad = new SimpleDateFormat("HH : mm");
    sdfmad.setTimeZone(TimeZone.getTimeZone(tz.getID()));

    Date inptdate = null;
    try {
        inptdate = sdfgmt.parse(ret);
    } catch (ParseException e) {e.printStackTrace();}

    String localString = sdfmad.format(inptdate);
    try {
        inptdate = sdfmad.parse(localString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return new Time(inptdate.getTime());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM