TL; DR
LocalDate.now() .toString()
2018-01-23
LocalDate.now() .plusDays( 10 ) .toString()
2018-02-02
Avoid obsolete time classes
The Calendar and GregorianCalendar classes are confusing, poorly designed, and complex. Avoid these classes and related old obsolete time classes. Now superseded by java.time classes.
java.time
today's date,
LocalDate
The LocalDate class represents a date value only without time and without a time zone.
The time zone is critical for determining the date. At any given moment, the date changes around the world by zone. For example, a few minutes after midnight in Paris, France is a new day, still "yesterday" in Montreal Quebec .
If no time zone is specified, the JVM implicitly applies the current default time zone. This default value may change at any time, so your results may vary. It is better to specify the desired / expected time zone explicitly as an argument.
Specify the time zone name in continent/region format, such as America/Montreal , Africa/Casablanca or Pacific/Auckland . Never use the abbreviation 3-4 letters, for example EST or IST , as they are not real time zones, and are not standardized and not even unique (!).
ZoneId z = ZoneId.of( "America/Montreal" ) ; LocalDate today = LocalDate.now( z ) ;
If you want to use the current time zone of JVMs, request it and pass as an argument. If this parameter is omitted, the default JVM is applied implicitly. It is better to be explicit, since the default value can be changed at any time during execution by any code in any thread of any application in the JVM.
ZoneId z = ZoneId.systemDefault() ;
Or enter a date. You can set the month to a number with a normal number of 1-12 for January-December.
LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ; // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.
Or, better, use Month predefined enumeration objects, one for each month of the year. Tip. Use these Month objects throughout your code base, not just an integer, to make your code more self-documenting, provide valid values, and provide type security .
LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;
Day of the week
day of the week
The DayOfWeek class represents a day of the week from Monday to Sunday. Pass these objects around your code, not just the integer 1-7.
DayOfWeek dow = ld.getDayOfWeek() ;
If you have a number, get 1-7 for Monday to Sunday according to ISO 8601.
int dowNumber = ld.getDayOfWeek().getValue() ;
Add days
the date when it will be in 100 days, the day of the week in one hundred days, my birthday and the day of the week and 10,000 days after my birthday and on that day of the week.
Adding and subtracting days is simple using the plus⦠and minus⦠methods.
LocalDate later = ld.plusDays( 10 ) ;
Alternatively, use Period to represent several years-months-days.
Period p = Period.ofDays( 10 ) ; LocalDate later = ld.plus( p ) ;
One hundred days or 10,000 days will be added in the same way.
LocalDate later = ld.plusDays( 10_000 ) ;
Numbering
Now I understand that GregorianCalendar starts from 0 on January and leaves on December 11
The java.time classes use robust numbering, unlike older classes.
- Number
2018 - 2018 year. No math since 1900 . - The months are numbered 1-12 for January-December.
- The days of the week are numbered 1-7 for Monday through Sunday, according to ISO 8601 .
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .
The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.
To learn more, see the Oracle Tutorial , and Stack Overflow for many examples and explanations. JSR 310 specification .
Where to get java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .