2012-09-13 45 views

回答

2
  • 使用SimpleDateFormat設置爲UTC時間
  • 格式使用SimpleDateFormat設置爲你所在的時區的解析Date值解析它(這很可能是「UTC + 8」以外的東西 - 你應該找出你的真的想要哪個TZDB時區ID

例如:

SimpleDateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy h:mma", Locale.US); 
inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"); 
Date date = inputFormat.parse(date + " " + time); 

// Or whatever format you want... 
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US); 
outputFormat.setTimeZone(targetTimeZone); 
String outputText = outputFormat.format(date); 

(如果你可以使用Joda Time代替,那簡直太好了 - 但我明白,這是一個Android應用程序相當大)

1

喬達時間庫提供了一組用於在多個時區中處理日期/時間的對象。 http://joda-time.sourceforge.net/

事情是這樣的,例如:

String date = "9/13/2012"; 
    String time = "5:48pm"; 

    String[] dateParts = date.split("/"); 
    Integer month = Integer.parseInt(dateParts[0]); 
    Integer day = Integer.parseInt(dateParts[1]); 
    Integer year = Integer.parseInt(dateParts[2]); 

    String[] timeParts = time.split(":"); 
    Integer hour = Integer.parseInt(timeParts[0]); 
    Integer minutes = Integer.parseInt(timeParts[1].substring(0,timeParts[1].lastIndexOf("p"))); 

    DateTime dateTime = new DateTime(year, month, day, hour, minutes, DateTimeZone.forID("Etc/GMT")); 
    dateTime.withZone(DateTimeZone.forID("Etc/GMT+8")); 
相關問題