将具有已知时区的日期对象转换为当前时区

时间:2017-03-02 10:26:40

标签: java android date timezone

所以我从Web服务器获取了一些日期对象,我知道服务器有时间在GMT +1(柏林),如何将日期对象转换为当前手机时区日期对象?

stackoverflow上的大多数问题只是关于时区内的格式化,而不是实际上像这样转换。

我试过这个

 Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT+1"));
    calendar.setTime(timeFromServer);
    Calendar calendar2 = new GregorianCalendar(TimeZone.getDefault());
    calendar2.setTimeInMillis(calendar.getTimeInMillis());

我打印时,calendar2.getTime()。toString()和timeFromServer.toString()将是相同的;

3 个答案:

答案 0 :(得分:0)

我使用Joda time并且它有效。您可以尝试Joda时间。此方法将时间从服务器转换为显示格式时间,并更改为相关的本地时间

public static final String SERVER_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DISPLAY_POST_FORMAT = "HH:mm dd/MM/yyyy";

public static String convertDateStrToDisplayFormat(String timeFromServer) {
        DateTimeFormatter serverFormatter = DateTimeFormat.forPattern(Constants.SERVER_FORMAT);
    serverFormatter.withZone(DateTimeZone.forID("Europe/Berlin"));
    DateTime dateTime = DateTime.parse(timeFromServer, serverFormatter);

    DateTimeFormatter pointTimeFormatter = DateTimeFormat.forPattern(Constants.POINT_TIME_FORMAT);
    pointTimeFormatter.withZone(DateTimeZone.forID("Europe/Berlin"));

    return pointTimeFormatter.print(dateTime)
    }

答案 1 :(得分:0)

我用它来从服务器转换日期并将其转换为当前手机时区日期对象

SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat deviceFormat = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
deviceFormat.setTimeZone(TimeZone.getDefault());
Date utcDate ;
Date deviceDate ;

utcDate = sourceFormat.parse(event_date);
deviceDate = deviceFormat.parse(utcDate.toString());

event_date是具有服务器日期的String。在此之后,您将在deviceDate上转换日期。

答案 2 :(得分:0)

java.util.Date不使用时区,因此当您尝试使用方法Date#toString()打印以下日期对象的字符串表示时,结果是相同的:

  • calendar2.getTime().toString()
  • timeFromServer.toString()

为了使用时区正确测试字符串表示,您需要使用SimpleDateFormat

SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
f.setTimeZone(calendar.getTimeZone());

// Date correctly printed with timezone:
System.out.println(f.parse(calendar.getTime()));

但是,你在问题中写的转换是正确的,这是我使用JUnit测试它的方式:

@Test
public void testDateConversion() throws ParseException {
  String serverText = "2017-03-02T11:54:30.207+01:00";

  SimpleDateFormat serverFmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
  serverFmt.setTimeZone(TimeZone.getTimeZone("GMT+1"));
  Date timeFromServer = serverFmt.parse(serverText);

  Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT-1"));
  calendar.setTime(timeFromServer);

  assertEquals(2017, calendar.get(Calendar.YEAR));
  assertEquals(Calendar.MARCH, calendar.get(Calendar.MONTH));
  assertEquals(2, calendar.get(Calendar.DAY_OF_MONTH));
  assertEquals(9, calendar.get(Calendar.HOUR_OF_DAY));
  assertEquals(54, calendar.get(Calendar.MINUTE));
  assertEquals(30, calendar.get(Calendar.SECOND));
  assertEquals(207, calendar.get(Calendar.MILLISECOND));

  SimpleDateFormat currFmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
  currFmt.setTimeZone(calendar.getTimeZone());

  System.out.printf("server_timestamp  = %d, server_date  = '%s', server_str  = '%s'%n",
      timeFromServer.getTime(),
      serverFmt.format(timeFromServer),
      timeFromServer.toString());
  System.out.printf("current_timestamp = %d, current_date = '%s', current_str = '%s'%n",
      calendar.getTime().getTime(),
      currFmt.format(calendar.getTime()),
      calendar.getTime().toString());
}

结果:

server_timestamp  = 1488452070207, server_date  = '2017-03-02T11:54:30.207+01:00', server_str  = 'Thu Mar 02 11:54:30 CET 2017'
current_timestamp = 1488452070207, current_date = '2017-03-02T09:54:30.207-01:00', current_str = 'Thu Mar 02 11:54:30 CET 2017'

另见: