Weird output after formatting the time in milliseconds [duplicate]

女生的网名这么多〃 提交于 2021-02-08 03:34:51

问题


I want to convert 1574348400 value to date format using code:

public class Main {

    public Main() {
        long value = 1574348400;
        String dateString = new SimpleDateFormat("EEEE dd MMMM, yyyy").format(new Date(value));
        System.out.println("Formated time: " + dateString);
    }

    public static void main(String[] args) {
        new Main();
    }
}

I want to get the output as: Wednesday 20 November, 2019 but I'm getting Monday 19 January, 1970. How to get the current date not the 1970's date?


回答1:


Parse your time (in seconds) using java.time, it provides a method for epoch seconds...

public static void main(String[] args) {
    // your seconds
    long seconds = 1574348400;
    // same in millis
    long millis = 1574348400000L;

    // find out the zone of your system
    ZoneId systemDefaultZoneId = ZoneId.systemDefault();
    // or set a specific one
    ZoneId utcZoneId = ZoneId.of("UTC");

    // parse a ZonedDateTime of your system default time zone from the seconds
    ZonedDateTime fromSecsSysDefZone = ZonedDateTime.ofInstant(Instant.ofEpochSecond(seconds),
                                                        systemDefaultZoneId);
    // parse a ZonedDateTime of UTC from the seconds
    ZonedDateTime fromSecsUtc = ZonedDateTime.ofInstant(Instant.ofEpochSecond(seconds),
                                                        utcZoneId);
    // parse a ZonedDateTime of your system default time zone from the milliseconds
    ZonedDateTime fromMillisSysDefZone = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis),
                                                        systemDefaultZoneId);
    // parse a ZonedDateTime of UTC from the milliseconds
    ZonedDateTime fromMillisUtc = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis),
                                                        utcZoneId);

    // print the ones that were created using your default time zone
    System.out.println("from seconds:\t"
            + fromSecsSysDefZone.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    System.out.println("from millis:\t"
            + fromMillisSysDefZone.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    // print a check for equality
    System.out.println("Both ZonedDateTimes are "
            + (fromSecsSysDefZone.equals(fromMillisSysDefZone) ? "equal" : "different"));

    System.out.println("————————————————————————————————");

    // print the ones that were created using UTC
    System.out.println("from seconds:\t"
            + fromSecsUtc.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    System.out.println("from millis:\t"
            + fromMillisUtc.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    // print a check for equality
    System.out.println("Both ZonedDateTimes are "
            + (fromSecsUtc.equals(fromMillisUtc) ? "equal" : "different"));
}

The output produced by this code (on my system) is

from seconds:   2019-11-21T16:00:00+01:00[Europe/Berlin]
from millis:    2019-11-21T16:00:00+01:00[Europe/Berlin]
Both ZonedDateTimes are equal
————————————————————————————————
from seconds:   2019-11-21T15:00:00Z[UTC]
from millis:    2019-11-21T15:00:00Z[UTC]
Both ZonedDateTimes are equal

If you have to use Java 6 or 7, then you can use the ThreeTenBackport-Project on Github, which enables (most) functionality of java.time in those two older versions.
Its use is explained on a separate website.




回答2:


Wrong value. Try:

long value = 1574348400000L;




回答3:


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

public class BasicWebCrawler {
    public BasicWebCrawler() {
    long value = 1574348400000L;
    Date date = new Date(value);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DATE, -1);
    Date minusOne = cal.getTime();
    String dateString = new SimpleDateFormat("EEEE dd MMMM, yyyy").format(minusOne);
    System.out.println("Formated time: " + dateString);
}

public static void main(String[] args) {
    new BasicWebCrawler();
}

}

output : Formated time: Wednesday 20 November, 2019




回答4:


Your first issue is: You are using seconds instead of milliseconds, new Date(long) the value of long is in milliseconds. See the Java 6 java.util.Date Documentation here

Your second issue is: When using Java 6 Date you need to know where the value in milliseconds was determined, if it's not in your timezone then you will need to make a conversion. Take the following code for example:

String zeroDateString = new SimpleDateFormat("EEEE dd MMMM, yyyy hh:mm").format(new Date(0));
System.out.println("Formated time -- zeroDateString = " + zeroDateString);

The output of new Date(0) in NYC, NY, USA will be Wednesday December 31, 1969 19:00 (the timezone of New-York City is EST which is GMT-05:00) while in Rome, Italy the output of the same code will be Thursday 01 January 1970 01:00 (the timezone of Rome, Italy is GMT+01:00)

If you need all your data to be according to GMT then you will need to make adjustment and/or calculation according to your timezone in relation to GMT.



来源:https://stackoverflow.com/questions/58949399/weird-output-after-formatting-the-time-in-milliseconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!