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
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
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.
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
Date date=null;
try {
date = formatter.parse("20130526160000 ");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
formatter = new SimpleDateFormat("dd MMM yyyy HH:mm");
System.out.println("Date :" +formatter.format(date));
you can try the following code to parse string to date..
java.util.Date date=null;
try {
date = new SimpleDateFormat("yyyymmddhhmmss", Locale.ENGLISH).parse("20130526160000");
System.out.println(date);// result will 'Sat Jan 26 16:00:00 IST 2013'
//now you can use Date class function i.e.
//date.getDay();
//date.getMonth();
//date.getYear();
} catch (ParseException e) {
System.out.println(">>>>>"+"date parsing exception");
//System.out.println(e.getMessage());
//e.printStackTrace();
}
Try this i think this will helps you.
long _date = Long.parseLong("20130526160000");
SimpleDateFormat _dateformat = new SimpleDateFormat("dd MMM yyyy hh:mm");
System.out.println("Date is:" + _dateformat.format(new Date(_date)));