How to convert string to datetime in android

I am trying to convert a string to a date. My line is like 20130526160000 . i need a date e.g. dd MMM yyyy hh: mm

e.g. May 26, 2013 16:00

+10
source share
5 answers

You can use SimpleDateFormat to parse 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 the required format, as shown below.

 sdf.applyPattern("dd MMM yyyy hh:mm"); System.out.println(sdf.format(d)); 
+34
source

You can use the following method.

 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); 

Closed: Wed May 15 10:00:00 IST 2013 Hope this helps you.

+11
source
 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)); 
+3
source

Try this, I think it will help 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))); 
+1
source

you can try the following code to parse a string for today.

 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 ie //date.getDay(); //date.getMonth(); //date.getYear(); } catch (ParseException e) { System.out.println(">>>>>"+"date parsing exception"); //System.out.println(e.getMessage()); //e.printStackTrace(); } 
0
source

Source: https://habr.com/ru/post/945676/


All Articles