We are moved to new domain
Click -> www.ehowtonow.com
Friday, 11 October 2013

How to convert String to Date Time format in Java


In this tutorial we are going to see how to convert String to Date Time format using SimpleDateFormat.
1. 12-10-2013 12:02:35 PM format to Util Date Time
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 12:02:35 PM";

try {
dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss a");
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 12:02:35 IST 2013

Date String : 12-10-2013 12:02:35 PM

2. 12-10-2013 12:02:35  format to Util Date Time

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 12:02:35";

try {
dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
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 12:02:35 IST 2013

Date String : 12-10-2013 12:02:35

3. Sat,12-10-2013 12:02:35  format to Util Date Time

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,12-10-2013 12:02:35";

try {
dateFormat = new SimpleDateFormat("E,dd-MM-yyyy HH:mm:ss");
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 12:02:35 IST 2013

Date String : Sat,12-10-2013 12:02:35

4. Saturday,12-10-2013 12:02:35  format to Util Date Time

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 = "Saturday,12-10-2013 12:02:35";

try {
dateFormat = new SimpleDateFormat("EEEE,dd-MM-yyyy HH:mm:ss");
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 12:02:35 IST 2013

Date String : Saturday,12-10-2013 12:02:35

5. SQL supported Date Time format

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 = "2013-10-31 12:22:35";

try {
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
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 : Thu Oct 31 12:22:35 IST 2013

Date String : 2013-10-31 12:22:35

6. SQL Date String to Java Date Time format

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 = "2013-10-31 12:22:35.0";

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

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


}

}

Output

Util Date : Thu Oct 31 12:22:00 IST 2013

SQL Date String : 2013-10-31 12:22:00.0

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to convert String to Date Time format in Java Rating: 5 Reviewed By: eHowToNow