Convert string to Date in java

I am trying to parse a string in a date field in an Android application, but I cannot get it correctly. Here is the line I'm trying to convert to the date "03/26/2012 11:49:00". I am using the function:

private Date ConvertToDate(String dateString){ SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa"); Date convertedDate = new Date(); try { convertedDate = dateFormat.parse(dateString); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return convertedDate; } 

But I keep getting 3/1/112 11:49AM as a result.

+54
java android date
Mar 30 2018-12-12T00:
source share
6 answers

You are mistaken in the way you show the data, which, I think, because for me:

  String dateString = "03/26/2012 11:49:00 AM"; SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa"); Date convertedDate = new Date(); try { convertedDate = dateFormat.parse(dateString); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(convertedDate); 

Print

 Mon Mar 26 11:49:00 EEST 2012 
+108
Mar 30 '12 at 14:40
source share

went fine when I used the Locale.US parameter in SimpleDateFormat

 String dateString = "15 May 2013 17:38:34 +0300"; System.out.println(dateString); SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z", Locale.US); DateFormat targetFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault()); String formattedDate = null; Date convertedDate = new Date(); try { convertedDate = dateFormat.parse(dateString); System.out.println(dateString); formattedDate = targetFormat.format(convertedDate); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(convertedDate); 
+17
Sep 21 '13 at 6:48 on
source share
 String str_date="13-09-2011"; DateFormat formatter ; Date date ; formatter = new SimpleDateFormat("dd-MM-yyyy"); date = (Date)formatter.parse(str_date); System.out.println("Today is " +date.getTime()); 

try it

+4
Mar 30 '12 at 14:50
source share

This code will help you make the result like FEB 17 20:49.

  String myTimestamp="2014/02/17 20:49"; SimpleDateFormat form = new SimpleDateFormat("yyyy/MM/dd HH:mm"); Date date = null; Date time = null; try { date = form.parse(myTimestamp); time = new Date(myTimestamp); SimpleDateFormat postFormater = new SimpleDateFormat("MMM dd"); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); String newDateStr = postFormater.format(date).toUpperCase(); String newTimeStr = sdf.format(time); System.out.println("Date : "+newDateStr); System.out.println("Time : "+newTimeStr); } catch (Exception e) { e.printStackTrace(); } 

Result:

Date: FEB 17

Time: 20:49

+2
Mar 27 '14 at 4:22
source share
 SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); String dateInString = "07/06/2013"; try { Date date = formatter.parse(dateInString); System.out.println(date); System.out.println(formatter.format(date)); } catch (ParseException e) { e.printStackTrace(); } 

Output:

 2014/08/06 16:06:54 2014/08/06 16:06:54 
+1
Nov 23 '15 at 19:04
source share
 GregorianCalendar date; CharSequence dateForMart = android.text.format.DateFormat.format("yyyy-MM-dd", date); Toast.makeText(LogmeanActivity.this,dateForMart,Toast.LENGTH_LONG).show(); 
0
Mar 23 '15 at 9:14
source share



All Articles