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
2. 12-10-2013 12:02:35 format to Util Date Time
Util Date : Sat Oct 12 12:02:35 IST 2013
Date String : 12-10-2013 12:02:35 PM
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
3. Sat,12-10-2013 12:02:35 format to Util Date Time
Util Date : Sat Oct 12 12:02:35 IST 2013
Date String : 12-10-2013 12:02:35
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
4. Saturday,12-10-2013 12:02:35 format to Util Date Time
Util Date : Sat Oct 12 12:02:35 IST 2013
Date String : Sat,12-10-2013 12:02:35
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
5. SQL supported Date Time format
Util Date : Sat Oct 12 12:02:35 IST 2013
Date String : Saturday,12-10-2013 12:02:35
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
6. SQL Date String to Java Date Time format
Util Date : Thu Oct 31 12:22:35 IST 2013
Date String : 2013-10-31 12:22:35
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
0 comments:
Post a Comment