简体   繁体   中英

how to date time in java

I have this code:

Date dt = new Date(100000000000L);
DateFormat[] dtformat = new DateFormat[6];
dtformat[0] = DateFormat.getInstance();
dtformat[1] = DateFormat.getDateInstance();
dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);
for(DateFormat dateform : dtformat)
   System.out.println(dateform.format(dt));

I don't know what is the meaning of this code (the 100000000000L ) function:

 Date dt = new Date(100000000000L);
 DateFormat[] dtformat = new DateFormat[6];

Can someone tell me? Because I want to replace the time to the 24 July 1998

The docs clearly state that the parameter for constructing a Date is:

milliseconds since January 1, 1970, 00:00:00 GMT not to exceed the milliseconds representation for the year 8099. A negative number indicates the number of milliseconds before January 1, 1970, 00:00:00 GMT.

So, 0 would represent Midnight 1 Jan, 1970.

However, if I were you, I would stop using Date and use LocalDate which is much easier to use.

Date(long date)

Its a parameterized constructor of Date Class in java which Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

For more information you can refer to below link Date Class in Java

john16384 in his answer says

if I were you, I would stop using Date and use LocalDate , which is much easier to use.

I agree. Here's how:

    LocalDate dt = LocalDate.of(1998, Month.JULY, 24);

    DateTimeFormatter[] dtformat = {
            DateTimeFormatter.BASIC_ISO_DATE,
            DateTimeFormatter.ISO_LOCAL_DATE,
            DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL),
            DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG),
            DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM),
            DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
    };

    for (DateTimeFormatter dateform : dtformat) {
        System.out.println(dt.format(dateform));
    }

Note how straightforward it is to create the desired date (to do the same with an old-fashioned Date object, you would be required to go through a class named Calendar ). I am using an array initializer instead of explicitly assigning an object to each element of the array. It's simpler.

On my computer (Danish locale) the above code prints:

19980724
1998-07-24
24. juli 1998
24. juli 1998
24-07-1998
24-07-98

If you want to control the locale explicitly (sometimes a good idea to avoid surprises), you may use DateTimeFormatter.withLocale() , for example DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withLocale(new Locale("Indonesian")) or DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withLocale(Locale.ROOT) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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