Java - Date

java

http://www.javaworld.com/article/2078757/java-se/java-101-the-next-generation-it-s-time-for-a-change.html - done reading
http://www.javaworld.com/article/2078757/java-se/java-101-the-next-generation-it-s-time-for-a-change.html?page=2

// Date format

Use java.util.Calendar instead of java.util.Date

// Format a date as string

(new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date())

import java.text.SimpleDateFormat;
import java.util.Date;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String timestamp = format.format(new Date());

// Parse a date string
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date ts = format.parse(strTimestamp);

// Determine the interval between two dates
Date ts = format.parse(this.strTimestamp);
Calendar cal = Calendar.getInstance();
long now = cal.getTimeInMillis();
cal.setTime(ts);
long timestamp = cal.getTimeInMillis();
long diff = now - timestamp;

// Obtain the current milliseconds since epoch
System.currentTimeMillis();

Why do some people prefer to use joda-time?

joda-time allows us to serialize DateTime objects to / from the database. Also, people prefer using joda-time because Interval, PeriodFormatter, and PeriodType are not available in Java 8.

Why should we or shouldn't we set the JVM time zone to UTC?

Set the JVM time zone to UTC so that all DateTime objects will be set to UTC. This helps greatly serializing to / from the database and not accidentally getting timezone conversions being applied when they shouldn't be.

How can we format a Date as a string?

import java.text.SimpleDateFormat;
import java.util.Date;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String timestamp = format.format(new Date());

How can we parse a date string?

SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date ts = format.parse(strTimestamp);

How can we know the interval between two dates?

Date ts = format.parse(this.strTimestamp);
Calendar cal = Calendar.getInstance();
long now = cal.getTimeInMillis();
cal.setTime(ts);
long timestamp = cal.getTimeInMillis();
long diff = now - timestamp;

How can we obtain the current milliseconds since epoch?

System.currentTimeMillis();

What is wrong with java.util.Date?

  • Constructors that accept year arguments require offsets from 1900, which has been a source of bugs.
  • January is represented by 0 instead of 1, also a source of bugs.
  • Date doesn't describe a date but describes a date-time combination.
  • Date's mutability makes it unsafe to use in multithreaded scenarios without external synchronization.
  • Date isn't amenable to internationalization.

Additionally, java.sql's Date, Time, and Timestamp classes extend java.util.Date and inherit its problems. Also, in order to prevent setting the time part in the Date class and the date part in the Time class, Date and Time force various setter methods to throw IllegalArgumentException, which is messy.

What is wrong with java.util.Calendar?

In an attempt to fix the flaws associated with java.util.Date, Sun introduced java.util.Calendar and related classes with the Java 1.1 release. Unfortunately, Calendar was also riddled with flaws, including the following:

  • It isn't possible to format a calendar.
  • January is represented by 0 instead of 1, a source of bugs.
  • Calendar isn't type-safe; for example, you must pass an int-based constant to the get(int field) method. (In fairness, enums weren't available when Calendar was released.)
  • Calendar's mutability makes it unsafe to use in multithreaded scenarios without external synchronization. (The companion java.util.TimeZone and java.text.DateFormat classes share this problem.)
  • Calendar stores its state internally in two different ways — as a millisecond offset from the epoch and as a set of fields — resulting in many bugs and performance issues.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License