Java SimpleDateFormat Examples

0

SimpleDateFormat is a concrete class which extends DateFormat. It is used for formatting and parsing dates in a locale-sensitive manner.

In this article, we will see examples of parsing and formatting using SimpleDateFormat.

If you want to see examples of DateFormat read here.

SimpleDateFormat parse Example

Let’s start with a parsing example. We construct a SimpleDateFormat using the pattern yyyy-MM-dd HH:mm:ss. We then parse a date string into a Date object calling SimpleDateFormat.parse().

SimpleDateFormatParseExample:

package com.javarticles.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class SimpleDateFormatParseExample {
    public static final void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = "2015-07-16 18:30:20";
        System.out.println("Parse " + dateStr + "->" + sdf.parse("2015-07-16 18:30:20"));
    }
}

Output:

Parse 2015-07-16 18:30:20->Thu Jul 16 18:30:20 IST 2015

Parsing does not necessarily use all characters up to the end of the string. In the below example, the date string has ‘is the date’ in the end.

package com.javarticles.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class SimpleDateFormatParsingPosExample {
    public static final void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = "2015-07-16 18:30:20 is the date";
        System.out.println("Parse '" + dateStr + "'->" + sdf.parse(dateStr));
    }
}

Output:

Parse '2015-07-16 18:30:20 is the date'->Thu Jul 16 18:30:20 IST 2015

Parse Date String Using ParsePosition

Suppose you want to parse the date string starting at some index, you need to call the overloaded parse(dateStr, parsePosition). The method attempts to parse text starting at the index given by
parsePosition.

SimpleDateFormatParsingPosExample:

package com.javarticles.date;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;

public class SimpleDateFormatParsingPosExample {
    public static final void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = "2015-07-16 18:30:20 is the date";
        System.out.println("Parse '" + dateStr + "'->" + sdf.parse(dateStr));
    }
}

Output:

Parse '2015-07-16 18:30:20 is the date'->Thu Jul 16 18:30:20 IST 2015

In the above example, the date string contains two date components. After the first date component is parsed, the ParsedPosition index is updated with the index after the last character used. We can use the updated ParsedPosition to parse the date string further to extract the second date component.

SimpleDateFormatParsingPosExample:

package com.javarticles.date;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;

public class SimpleDateFormatParsingPosExample {
    public static final void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = "2015-07-16 18:30:20 is the date";
        System.out.println("Parse '" + dateStr + "'->" + sdf.parse(dateStr));
        
        dateStr = "Date is 2015-07-16 18:30:20 2012-07-14 05:20:25";        
        ParsePosition pos = new ParsePosition("Date is".length());
        System.out.println("Parse '" + dateStr + "' from index " + pos.getIndex());
        System.out.println("First date: " + sdf.parse(dateStr, pos));
        System.out.println("After pos is " + pos.getIndex() + ", parsing starts for:" + dateStr.substring(pos.getIndex()));
        System.out.println("Second date: " + sdf.parse(dateStr, pos));
    }
}

Output:

Parse '2015-07-16 18:30:20 is the date'->Thu Jul 16 18:30:20 IST 2015
Parse 'Date is 2015-07-16 18:30:20 2012-07-14 05:20:25' from index 7
First date: Thu Jul 16 18:30:20 IST 2015
After pos is 27, parsing starts for: 2012-07-14 05:20:25
Second date: Sat Jul 14 05:20:25 IST 2012

Format a Date into a date/time string for different time zones

In this example, we will convert the Date object to date/time string for different time zones. We will also set the time zone for the calendar using SimpleDateFormat.setTimeZone().

SimpleDateFormatTimeZoneExample:

package com.javarticles.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class SimpleDateFormatTimeZoneExample {
    public static final void main(String[] args) throws ParseException {
        String dateStr = "2015-07-16 18:30:20";
        Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
        
        System.out.println(dateStr + " in NZ->" + timezoneDate(date, TimeZone.getTimeZone("NZ")));
        System.out.println(dateStr + " in US/Eastern->" + timezoneDate(date, TimeZone.getTimeZone("US/Eastern")));
        System.out.println(dateStr + " in IST->" + timezoneDate(date, TimeZone.getTimeZone("IST")));
    }
    
    public static synchronized String timezoneDate(Date date,
            TimeZone timeZone) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        formatter.setTimeZone(timeZone);
        return formatter.format(date);
    }
}

Output:

2015-07-16 18:30:20 in NZ->2015-07-17 01:00:20
2015-07-16 18:30:20 in US/Eastern->2015-07-16 09:00:20
2015-07-16 18:30:20 in IST->2015-07-16 18:30:20

Download the source code

This was an example about Java SimpleDateFormat.

You can download the source code here: javaSimpleDataFormatExample.zip

About Author

Ram’s expertise lies in test driven development and re-factoring. He is passionate about open source technologies and loves blogging on various java and open-source technologies like spring.
You can reach him at [email protected]

Comments are closed.