Creating Date objects
suggest changeDate date = new Date();
System.out.println(date); // the current instant, in the JVM default time zone
This Date holds the instant when it was created. Date.toString() prints that instant in the JVM's default time zone, so the text is not the same on every machine.
Most Date constructors are deprecated. Build a specific date with a Calendar, then call getTime(). Calendar.set(int year, int month, int day) takes the full year: 1990, not 90 (90 is year 90). Months are zero-based, so use the Calendar constants. clear() drops the current time of day; without it, hour, minute, second, and millisecond stay at whatever they were.
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(1990, Calendar.DECEMBER, 11);
Date myBirthDate = calendar.getTime();
System.out.println(myBirthDate); // 1990-12-11 00:00:00 in the default time zone
The six-argument set also takes hour, minute, and second. After clear(), the millisecond stays 0.
calendar.set(1990, Calendar.DECEMBER, 11, 8, 32, 35);
Date myBirthDatenTime = calendar.getTime();
System.out.println(myBirthDatenTime); // 1990-12-11 08:32:35 in the default time zone
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents