TL; DR
LocalDate.now() .toString()
2017-01-23
It is better to explicitly indicate the desired / expected time zone.
LocalDate.now( ZoneId.of( "America/Montreal" ) ) .toString()
java.time
The modern approach to Java 8 and later consists of the java.time framework.
Specify the time zone, as the date changes worldwide at any time.
ZoneId zoneId = ZoneId.of( "America/Montreal" ) ; // Or ZoneOffset.UTC or ZoneId.systemDefault() LocalDate today = LocalDate.now( zoneId ) ; String output = today.toString() ;
2017-01-23
By default, you get a string in the standard ISO 8601 format .
For other formats, use the java.time.format.DateTimeFormatter class.
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old time classes such as java.util.Date , .Calendar and java.text.SimpleDateFormat .
The Joda-Time project, now in maintenance mode , advises switching to java.time.
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations.
Most of the functionality of java.time is ported back to Java 6 and 7 in ThreeTen-Backport and then adapted to Android in ThreeTenABP (see How to use ... ).
The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter , etc.
Basil Bourque Apr 19 '16 at 7:51 2016-04-19 07:51
source share