Friday, 11 October 2013

How to Convert String to Date in Java


In this tutorial we are going to see how to convert String to Date using SimpleDateFormat.
SimpleDateFormat is a concrete class for formatting and parse dates in a locale-sensitive manner. It allows for formatting date to text, parsing text to date an d normalization.
Date and Time Patterns
Below table contains characters used in SimpleDateFormat  for date time conversion.
Pattern
Description
Example
G Era designator AD
y Year 2013; 96
Y Week year 2013; 09
M Month in year July; Jul; 07
w Week in year 25
W Week in month 3
D Day in year 155
d Day in month 10
F Day of week in month 3
E Day name in week Tuesday; Tue
u Day number of week (1 = Monday, ..., 7 = Sunday) 2
a Am/pm marker PM
H Hour in day (0-23) 0
k Hour in day (1-24) 24
K Hour in am/pm (0-11) 0
h Hour in am/pm (1-12) 12
m Minute in hour 30
s Second in minute 56
S Millisecond 786
z Time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone -0800
X Time zone -08; -0800; -08:00
Date Format Conversions
1. 12-Oct-2013 format to Java Util Date
package com.javatutorialscorner.util;

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

public class StringToDateConvertion {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

SimpleDateFormat dateFormat = null;
String inputDate = "12-Oct-2013";

try {
dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date utilDate = dateFormat.parse(inputDate);
System.out.println("Util Date : "+utilDate);
String dateString = dateFormat.format(utilDate);
System.out.println("Date String : "+dateString);

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

In above example month format ‘M’ used in three time.This will interpret month as String.If we use less than three M month will be interpreted as number.

Output

Util Date : Sat Oct 12 00:00:00 IST 2013

Date String : 12-Oct-2013

2. 12-10-2013 format to Java Util Date

package com.javatutorialscorner.util;

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

public class StringToDateConvertion {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

SimpleDateFormat dateFormat = null;
String inputDate = "12-10-2013";

try {
dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date utilDate = dateFormat.parse(inputDate);
System.out.println("Util Date : "+utilDate);
String dateString = dateFormat.format(utilDate);
System.out.println("Date String : "+dateString);

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

here ‘M’ used in two times so month interpreted as number

Output

Util Date : Sat Oct 12 00:00:00 IST 2013

Date String : 12-10-2013

3. 12/10/2013 format to Java Util Date

package com.javatutorialscorner.util;

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

public class StringToDateConvertion {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

SimpleDateFormat dateFormat = null;
String inputDate = "12/10/2013";

try {
dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date utilDate = dateFormat.parse(inputDate);
System.out.println("Util Date : "+utilDate);
String dateString = dateFormat.format(utilDate);
System.out.println("Date String : "+dateString);

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

Output

Util Date : Sat Oct 12 00:00:00 IST 2013

Date String : 12/10/2013

4. Oct 12, 2013 format to Java Util Date

package com.javatutorialscorner.util;

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

public class StringToDateConvertion {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

SimpleDateFormat dateFormat = null;
String inputDate = "Oct 12, 2013";

try {
dateFormat = new SimpleDateFormat("MMM dd,yyyy");
Date utilDate = dateFormat.parse(inputDate);
System.out.println("Util Date : "+utilDate);
String dateString = dateFormat.format(utilDate);
System.out.println("Date String : "+dateString);

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

Output

Util Date : Sat Oct 12 00:00:00 IST 2013

Date String : Oct 12,2013

5. Sat,Oct 12 2013 format to Java Util Date

package com.javatutorialscorner.util;

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

public class StringToDateConvertion {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

SimpleDateFormat dateFormat = null;
String inputDate = "Sat,Oct 12 2013";

try {
dateFormat = new SimpleDateFormat("E,MMM dd yyyy");
Date utilDate = dateFormat.parse(inputDate);
System.out.println("Util Date : "+utilDate);
String dateString = dateFormat.format(utilDate);
System.out.println("Date String : "+dateString);

} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

Output

Util Date : Sat Oct 12 00:00:00 IST 2013

Date String : Sat,Oct 12 2013

0 comments

Post a Comment