简体   繁体   English

在Java中将日期从UTC转换为EST?

[英]Converting date from UTC to EST in Java?

I'm trying to convert a long timestamp that is UTC to Eastern Standard Time and am totally lost. 我正在尝试将UTC的长时间戳转换为东部标准时间并完全丢失。 Any hints would be great! 任何提示都会很棒!

Time format should be : 11/4/03 8:14 PM Thanks in advance! 时间格式应为:11/4/03 8:14 PM在此先感谢!

TimeZone utcTZ= TimeZone.getTimeZone("UTC");
Calendar utcCal= Calendar.getInstance(utcTZ);
utcCal.setTimeInMillis(utcAsLongValue);



import java.text.SimpleDateFormat;
import java.util.Date;

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
sdf.setTimeZone(utcTZ);
Date utcDate= utcCal.getTime();
sdf.formatDate(utcDate);

Yesterday occasionally I wrote the following method that can help you: 昨天偶尔我写了以下方法,可以帮助你:

private Date shiftTimeZone(Date date, TimeZone sourceTimeZone, TimeZone targetTimeZone) {
    Calendar sourceCalendar = Calendar.getInstance();
    sourceCalendar.setTime(date);
    sourceCalendar.setTimeZone(sourceTimeZone);

    Calendar targetCalendar = Calendar.getInstance();
    for (int field : new int[] {Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND}) {
        targetCalendar.set(field, sourceCalendar.get(field));
    }
    targetCalendar.setTimeZone(targetTimeZone);

    return targetCalendar.getTime();
}

Now you just have to format the date. 现在你只需格式化日期。 Use SimpleDateFormat for this. 为此使用SimpleDateFormat。 Here is the example: 这是一个例子:

DateFormat format = new SimpleDateFormat("dd/MM/yy hh:mm a");
format.format(date);

You should not think in terms of converting a Date to a different timezone. 您不应该考虑将日期转换为不同的时区。 Dates in Java are, and should always be, in UTC. Java中的日期是,而且应该始终是UTC。

Rather, you can set a particular timezone when you want to format a Date. 相反,您可以在要格式化日期时设置特定时区。 Here is an example: 这是一个例子:

public static void main(String[] args) throws Exception {

    String tzid = "EST";
    TimeZone tz = TimeZone.getTimeZone(tzid);

    long utc = System.currentTimeMillis();  // supply your timestamp here
    Date d = new Date(utc);

    // timezone symbol (z) included in the format pattern for debug
    DateFormat format = new SimpleDateFormat("yy/M/dd hh:mm a z");

    // format date in default timezone
    System.err.println(format.format(d));

    // format date in target timezone
    format.setTimeZone(tz);
    System.err.println(format.format(d));

}

Output for me (my default timezone is GMT): 我的输出(我的默认时区是GMT):

11/12/19 10:06 AM GMT
11/12/19 05:06 AM EST

Alternatively, you can set the timezone on a Calendar, and then access the Calendar fields you require. 或者,您可以在日历上设置时区,然后访问所需的日历字段。 For example: 例如:

    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("EST"));
    c.setTimeInMillis(utc);
    System.err.printf("%d/%d/%d %d:%d %s\n", c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR), c.get(Calendar.MINUTE), (c.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM"));

(This does not give you the exact pattern you requested.) (这不会为您提供您所要求的确切模式。)

Timezone conversion can be tricky. 时区转换可能很棘手。 You should probably use Joda Time where timezone tables are constantly updated (and can be updated manually if needed 您应该使用Joda Time来不断更新时区表(如果需要,可以手动更新)

Using Joda time it is pretty easy: 使用Joda时间非常简单:

public static Date convertJodaTimezone(LocalDateTime date, String localTimeZone, String destTimeZone) {
  DateTime srcDateTime = date.toDateTime(DateTimeZone.forID(localTimeZone));
  DateTime dstDateTime = srcDateTime.withZone(DateTimeZone.forID(destTimeZone));
  return dstDateTime.toLocalDateTime().toDateTime().toDate();
}

with timezones from the following table 使用下表中的时区

Under the hood Java keeps date value in UTC milliseconds so I think only way to shift value is by manipulating these millies eg 在引擎盖下,Java以UTC毫秒保持日期值,所以我认为只有通过操纵这些毫秒来改变价值的方法

private void getShiftedDate(Date date, TimeZone targetZone) {
    long sourceMillies = TimeUnit.MILLISECONDS.toMillis(TimeZone.getDefault().getRawOffset());
    long targetMillies = TimeUnit.MILLISECONDS.toMillis(targetZone.getRawOffset());

    long newMillies = date.getTime() + (targetMillies - sourceMillies);
    Date shiftedDate = new Date(newMillies);
    System.out.println("shifted => " + shiftedDate);
}

However, toString() of shiftedDate will still print default/source time zone string. 但是,shiftedDate的toString()仍将打印默认/源时区字符串。

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

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