19

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

Suraj
  • 243
  • 1
  • 2
  • 10

5 Answers5

54

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));
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
Obl Tobl
  • 5,604
  • 8
  • 41
  • 65
26

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.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • 2
    How can i get the output like - "15-05-2015" and stored into date variable and when i toast the date then it must show the output ; "15-05-2015" –  Oct 26 '16 at 14:43
5
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));  
prvn
  • 916
  • 5
  • 5
0

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();
        }
Mukesh Kumar Singh
  • 4,512
  • 2
  • 22
  • 30
0

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)));
android_dev
  • 1,477
  • 12
  • 18