new Date(long) gives different results

后端 未结 3 2063
孤独总比滥情好
孤独总比滥情好 2020-12-20 13:12

When I run this code:

System.out.println( \"XXX date=\" + new Date( 1311781583373L ) );

I get this result in Eclipse\'s JUnit runner:

相关标签:
3条回答
  • 2020-12-20 13:53

    Add:

    System.out.println(TimeZone.getDefault().getDisplayName());
    

    before calculating the date. It should display different time zones. And the default time zone is based on the locale your JVM uses. You can force the JVM to use the locale of your preference by supplying it with the following parameters:

    $ java -Duser.language=fr -Duser.country=CA
    

    In Maven you can use MAVEN_OPTS environment variable. Also here is an article describing how to change your locale permanently on Windows.

    0 讨论(0)
  • 2020-12-20 14:06

    To specify the default timezone you can set the system property user.timezone. You can do this by passing it as an argument to the JavaVM (you may need to modify eclipse.ini or Maven equivalent config file):

    -Duser.timezone=<your preferred timezone>
    

    ...or by setting it programmatically:

     System.setProperty("user.timezone", "<your preferred timezone>");
    

    Or, if it is convenient you can specify a timezone which you use each time you print out the date:

    DateFormat myDateFormat = new SimpleDateFormat("<insert date format string here>");    
    myDateFormat.setTimeZone(TimeZone.getTimeZone("<your preferred timezone>")); 
    ....
    System.out.println(myDateFormat.format(yourDate));
    
    0 讨论(0)
  • 2020-12-20 14:10

    Looks like Maven and Eclipse have picked up different default time zones, that's all.

    Don't forget that Date.toString() uses the default time zone. I would personally prefer to use Joda Time and probably log the UTC value instead of a local time :)

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