INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Text Formatting

Date Formatting and Parsing by stephendfrancis
Posted: 2 Oct 01

In most languages, date parsing and formatting (converting strings to/from an internal date format) can be a complicated business. Java provides several APIs to help:

java.text.DateFormat
java.text.SimpleDateFormat
java.util.Date
java.util.Calendar
java.util.TimeZone

strictly speaking, only the first two are concerned with representing dates as strings. They both have methods:

String format( java.util.Date )
java.util.Date parse( String )

to move between a String object and a Date object representation of a date. The difference is that whilst the DateFormat uses the standard date formatting of the current locale, the SimpleDateFormat uses a custom format defined by special formatting characters commonly used in other languages.

E.g.

import java.text.*;

SimpleDateFormat df1 = new SimpleDateFormat( "dd/MM/yy" );
SimpleDateFormat df2 = new SimpleDateFormat( "dd-MMM-yyyy" );

...

java.util.Date dDate = df1.parse( strInput );
String strOutput = df2.format( dDate );

This will turn e.g. "12/6/97" into "12-Jun-1997".

The full documentation of the SimpleDateFormat characters is in the J2SE API document, package java.text, class SimpleDateFormat.

Points to note:

1. The parse() method does not (by default) throw an Exception if the date is correctly formatted but invalid on the calendar (e.g. 29/2/2001), instead it alters the date to be a valid one (in the previous example, to 1/3/2001). If this isn't what you want, call df.setLenient( false ).

2. If the format represents a 4-digit year (yyyy) then "12/6/01" is parsed as "12/06/0001" - 1 A.D. If it is 2-digit (yy) then the year is set to be between 80 years before and 20 years after the date the SimpleDateFormat instance is created (e.g. today!). Thus if today is 1/10/01, then "30/9/21" is interpreted as "30/09/2021" and "1/10/22" is "01/10/1922". This boundary date can be altered by the set2DigitYearStart() method.

3. Years are only interpreted as per (2) if 2 digits are supplied. "1/1/1" will always be interpreted as "01/01/0001".

4. If you are using SQL (thus importing the java.sql.* package) you need to always explicitly reference the Date object as java.util.Date, since both util and sql packages contain objects called Date.

The TimeZone, Calendar and its child GregorianCalendar can do more sophisticated date arithmetic and processing, but do not deal directly with dates formatted as strings.

If you want to allow the user to enter either 2-digit or 4-digit years, try this:

static java.util.Date parseDate( String strDate ) {
  SimpleDateFormat df4 = new SimpleDateFormat( "dd/MM/yyyy" );
  SimpleDateFormat df2 = new SimpleDateFormat( "dd/MM/yy" );

  java.util.Date dOut;
  df2.setLenient( false );
  df4.setLenient( false );
  try {
    dOut = df2.parse( strDate );
  } catch ( ParseException e ) {
    dOut = df4.parse( strDate );
  }
  return dOut;
}

Back to Java FAQ Index
Back to Java Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close