How to set time zone of a java.util.Date?

后端 未结 10 1759
我寻月下人不归
我寻月下人不归 2020-11-22 01:45

I have parsed a java.util.Date from a String but it is setting the local time zone as the time zone of the date object.

The ti

相关标签:
10条回答
  • 2020-11-22 02:09

    You could also set the timezone at the JVM level

    Date date1 = new Date();
    System.out.println(date1);
    
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    // or pass in a command line arg: -Duser.timezone="UTC"
    
    Date date2 = new Date();
    System.out.println(date2);
    

    output:

    Thu Sep 05 10:11:12 EDT 2013
    Thu Sep 05 14:11:12 UTC 2013
    
    0 讨论(0)
  • 2020-11-22 02:09

    Here you be able to get date like "2020-03-11T20:16:17" and return "11/Mar/2020 - 20:16"

     private String transformLocalDateTimeBrazillianUTC(String dateJson) throws  ParseException {
        String localDateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss";
        SimpleDateFormat formatInput = new SimpleDateFormat(localDateTimeFormat);
    
        //Here is will set the time zone
        formatInput.setTimeZone(TimeZone.getTimeZone("UTC-03"));
    
        String brazilianFormat = "dd/MMM/yyyy - HH:mm";
        SimpleDateFormat formatOutput = new SimpleDateFormat(brazilianFormat);
        Date date = formatInput.parse(dateJson);
        return formatOutput.format(date);
    }
    
    0 讨论(0)
  • 2020-11-22 02:11

    Be aware that java.util.Date objects do not contain any timezone information by themselves - you cannot set the timezone on a Date object. The only thing that a Date object contains is a number of milliseconds since the "epoch" - 1 January 1970, 00:00:00 UTC.

    As ZZ Coder shows, you set the timezone on the DateFormat object, to tell it in which timezone you want to display the date and time.

    0 讨论(0)
  • 2020-11-22 02:12

    If you must work with only standard JDK classes you can use this:

    /**
     * Converts the given <code>date</code> from the <code>fromTimeZone</code> to the
     * <code>toTimeZone</code>.  Since java.util.Date has does not really store time zome
     * information, this actually converts the date to the date that it would be in the
     * other time zone.
     * @param date
     * @param fromTimeZone
     * @param toTimeZone
     * @return
     */
    public static Date convertTimeZone(Date date, TimeZone fromTimeZone, TimeZone toTimeZone)
    {
        long fromTimeZoneOffset = getTimeZoneUTCAndDSTOffset(date, fromTimeZone);
        long toTimeZoneOffset = getTimeZoneUTCAndDSTOffset(date, toTimeZone);
    
        return new Date(date.getTime() + (toTimeZoneOffset - fromTimeZoneOffset));
    }
    
    /**
     * Calculates the offset of the <code>timeZone</code> from UTC, factoring in any
     * additional offset due to the time zone being in daylight savings time as of
     * the given <code>date</code>.
     * @param date
     * @param timeZone
     * @return
     */
    private static long getTimeZoneUTCAndDSTOffset(Date date, TimeZone timeZone)
    {
        long timeZoneDSTOffset = 0;
        if(timeZone.inDaylightTime(date))
        {
            timeZoneDSTOffset = timeZone.getDSTSavings();
        }
    
        return timeZone.getRawOffset() + timeZoneDSTOffset;
    }
    

    Credit goes to this post.

    0 讨论(0)
提交回复
热议问题