问题
I am using SimpleDateFormat
class to set pattern and using the parse
method to parse the String
to Date
object.
But when I am printing the Date
object without using method format()
:
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd");
Date dat1 = format.parse("2017-11-01");
System.out.println(dat1);
My result is:
Sun Dec 30 00:00:00 UTC 1990
回答1:
tl;dr
LocalDate.parse( "2017-11-01" )
Results not reproducible
Your results cannot be reproduced. See code run live at IdeOne.com:
SimpleDateFormat format = new SimpleDateFormat( "YYYY-MM-dd" );
Date dat1 = format.parse( "2017-11-01" );
System.out.println( dat1 );
Sun Jan 01 00:00:00 GMT 2017
java.time
You are using troublesome old legacy classes now supplanted by the java.time.
LocalDate ld = LocalDate.parse( "2017-11-01" )
ld.toString(): 2017-11-01
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.
来源:https://stackoverflow.com/questions/47477718/printing-date-object-without-using-format-method