Printing the correct date using the Java GregorianCalendar class

I am trying to print several things, such as today's date, day of the week, date when it will be in 100 days, day of the week in one hundred days, my birthday and day of the week, and 10,000 days after my birthday and on that day of the week. Now I understand that GregorianCalendar starts from January 0 through December 11th. I get this, so it makes sense that when I try to print the date that is mentioned today, the date is 8/25/12, not 9/25/12, but I have no idea how to fix this without specifying the date month ahead, and then actually puts the month in October, not September.

Here is what I mean at the moment.

GregorianCalendar cal = new GregorianCalendar(); int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); int month = cal.get(Calendar.MONTH); int year = cal.get(Calendar.YEAR); int weekday = cal.get(Calendar.DAY_OF_WEEK); cal.add(Calendar.DAY_OF_MONTH, 100); int dayOfMonth2 = cal.get(Calendar.DAY_OF_MONTH); int month2 = cal.get(Calendar.MONTH); int year2 = cal.get(Calendar.YEAR); int weekday2 = cal.get(Calendar.DAY_OF_WEEK); GregorianCalendar birthday = new GregorianCalendar(1994, Calendar.JANUARY, 1); int dayOfMonth3 = birthday.get(Calendar.DAY_OF_MONTH); int month3 = birthday.get(Calendar.MONTH); int year3 = birthday.get(Calendar.YEAR); int weekday3 = birthday.get(Calendar.DAY_OF_WEEK); birthday.add(Calendar.DAY_OF_MONTH, 10000); int weekday4 = birthday.get(Calendar.DAY_OF_WEEK); int dayOfMonth4 = birthday.get(Calendar.DAY_OF_MONTH); int month4 = birthday.get(Calendar.MONTH); int year4 = birthday.get(Calendar.YEAR); System.out.printf("Todays date is " +month + "/" +dayOfMonth +"/" +year +"."); System.out.printf(" It is day " +weekday +" of the week"); System.out.println(""); System.out.printf("In 100 days it will be " +month2 + "/" +dayOfMonth2 +"/" +year2 +". "); System.out.printf("Day " +weekday2 +" of the week"); System.out.println(""); System.out.printf("My Birthday is " +month3 + "/" +dayOfMonth3 +"/" +year3 +". "+"Day " +weekday3 +" of the week"); System.out.println(""); System.out.printf("10,000 days after my birthday is " +month4 + "/" +dayOfMonth4 +"/" +year4 +". " +"Day " +weekday4 +" of the week"); 

So, I need help adjusting the month for today, the date of 100 days, the date of my birthday and 10,000 days after my birthday. Any help or understanding is greatly appreciated.

+4
source share
3 answers

I get this, so it makes sense that when I try to print the date she is talking about, today the date is 8/25/12, not 9/25/12, but I have no idea how to fix this without setting date ahead extra month

If you are going to print a month by doing

 int month = cal.get(Calendar.MONTH); ... System.out.printf("Todays date is " + month + ... 

Then you want to print month + 1 , not just month .

Ultimately, although you are going to save a lot more time and headache, simply using SimpleDateFormat to format dates as strings.

+6
source

Yes, you need to know that Calendar.JANUARY is zero. Months are based on a zero value for the Calendar.

You work too much here. You work too much with primitives.

Here's how to print today's date:

 DateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd"); formatter.setLenient(false); Date today = new Date(); System.out.println(formatter.format(today)); 

Here's how to get 100 days from now:

 Calendar calendar = Calendar.getInstance(); calendar.setTime(today); calendar.add(Calendar.DAY_OF_YEAR, 100); Date oneHundredDaysFromToday = calendar.getTime(); System.out.println(formatter.format(oneHundredDaysFromToday)); 

Stop working with all of these int values.

+4
source

TL; DR

 LocalDate.now() .toString() // Expecting January 23, 2018. 

2018-01-23

 LocalDate.now() .plusDays( 10 ) .toString() // Expecting February 2, 2018. 

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() ; // Get JVM's current default time zone. 

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() ; // Number 1-7 for Monday-Sunday. 

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 ) ; // Ten days later. 

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 .

0
source

Source: https://habr.com/ru/post/1436256/


All Articles