I trying to convert string to date. My String like 20130526160000 . i want date like dd MMM yyyy hh:mm
e.g.-26 May 2013 16:00
PySpark SQL function provides to_date() function to convert String to Date fromat of a DataFrame column. Note that Spark Date Functions support all Java Date formats specified in DateTimeFormatter. to_date() – function is used to format string ( StringType ) to date ( DateType ) column.
You can use SimpleDateFormat
for parsing String to Date.
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
try {
Date d = sdf.parse("20130526160000");
} catch (ParseException ex) {
Log.v("Exception", ex.getLocalizedMessage());
}
Now you can convert your Date
object back to String in your required format as below.
sdf.applyPattern("dd MMM yyyy hh:mm");
System.out.println(sdf.format(d));
You can use the following way.
String strDate = "2013-05-15T10:00:00-0700";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
Date date = dateFormat.parse(strDate);
System.out.println(date);
Output is : Wed May 15 10:00:00 IST 2013
I hope this will help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With