When using the constructor of Java's Date class, the date advances by 1900.

I'm really reminded now, but I'm addicted to it ...

Introduction

When I was writing a program to format a date into a string, I encountered a phenomenon that the number of years advanced by 1900 for some reason. In the first place, the constructor that takes the "year, month, day" of the Date class is deprecated, so it is certainly a story that you do not have to use it. However, it is an example of a failure that happens when you use it carelessly without knowing it.

phenomenon

DateTest.java


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTest {
  public static void main(String[] args) {
    int year = 2018;
    int month = 5;
    int dayOfMonth = 6;
    Date todayDate = new Date(year, month, dayOfMonth);
    DateFormat dateFormat = SimpleDateFormat.getDateInstance();
    System.out.println(dateFormat.format(todayDate));
  }
}

As in this example, if you create a Date instance based on the date given by the ʻint type and create aFormatwithDateFormat`,

3918/06/06

This year is 2018, but it is 3918. It's just 1900.

Reason

This is a specification of the java.util.Date class. Also written in Date (Java Platform SE 8) However, the value of year received by this constructor is the implementation that takes the value obtained by subtracting 1900 from the year of the calendar.

In the first place ...

As is well known to those who are familiar with it, the constructor that takes the date of java.util.Date is deprecated from JDK1.1 (it appears when you search for Date, so I'm likely to use it). Instead, you should instantiate it with java.util.Calendar and then get a Date instance with Calendar # getDate.

DateTest.java


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateTest {
  public static void main(String[] args) {
    int year = 2018;
    int month = 5;
    int dayOfMonth = 6;
    Calendar todayDate = Calendar.getInstance();
    todayDate.set(year, month, dayOfMonth);
    DateFormat dateFormat = SimpleDateFormat.getDateInstance();
    System.out.println(dateFormat.format(todayDate.getTime()));
  }
}

2018/07/06

The date is now output normally.

Recommended Posts

When using the constructor of Java's Date class, the date advances by 1900.
Output of the day of the week using Ruby's Date class
[Ruby] Display today's day of the week using Date class
Use of Date class
[Kotlin] Get the argument name of the constructor by reflection
When the month of the date is acquired, the January shift
Limit the number of threads using Java's Executor Service
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
[Rails] Sort the post list by date or number of likes using the pull-down box
Getting a little stuck on the last day of the month using Java's Calendar class
[Enum] Let's improve the readability of data by using rails enum
[Rails] Register by attribute of the same model using Devise
Example of using abstract class
When using FloatingPanel, the tableView created by Stroybard becomes nil
[Rails] How to convert the URI of the image sent by http to https when using Twitter API
I want to judge the necessity of testing by comparing the difference of class files when refactoring Java
Switch the version of java installed by SDKMAN when moving directories
The design concept of Java's Date and Time API is interesting
Easy way to create a mapping class when using the API
When using JDBC, NLS parameters are affected by the Java locale.
Get date without using Calendar class
Various methods of the String class
Error when the member of Entity class used in SpringWebFlux is final
[Java] How to get to the front of a specific string using the String class
From Java9, the constructor of the class corresponding to primitive types is deprecated.
Display Japanese calendar and days of the week using java8 standard class
When the bean class name is duplicated
Try using || instead of the ternary operator
Test the integrity of the aggregation using ArchUnit ②
The process of understanding Gemfile by non-engineers
[Swift] Termination of the program by assertion
Reading a file using Java's Scanner class
The contents of the data saved by CarrierWave.
Sort by multiple fields in the class
[Java] Get the date with the LocalDateTime class
Test the integrity of the aggregation using ArchUnit ③
Call a method of the parent class by explicitly specifying the name in Ruby
Get the class name and method name of Controller executed by HandlerInterceptor of Spring Boot
Pay attention to the boundary check of the input value when using the float type
[Java] Calculate the day of the week from the date (Calendar class is not used)