Convertir une chaîne en date en Java

J’essaie d’parsingr une chaîne dans un champ de date dans une application Android, mais je n’arrive pas à le comprendre. Voici la chaîne que j’essaie de convertir en date “26/03/2012 11:49:00”. La fonction que j’utilise est la suivante:

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

Mais je continue à avoir comme résultat 3/1/112 11:49 AM. Toute aide que j’apprécierais vraiment. Merci

Vous avez tort dans la manière dont vous affichez les données, car pour moi:

  Ssortingng dateSsortingng = "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(dateSsortingng); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(convertedDate); 

Impressions:

 Mon Mar 26 11:49:00 EEST 2012 

ça allait bien quand Locale.US paramètre Locale.US dans SimpleDateFormat

 Ssortingng dateSsortingng = "15 May 2013 17:38:34 +0300"; System.out.println(dateSsortingng); SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z", Locale.US); DateFormat targetFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault()); Ssortingng formattedDate = null; Date convertedDate = new Date(); try { convertedDate = dateFormat.parse(dateSsortingng); System.out.println(dateSsortingng); formattedDate = targetFormat.format(convertedDate); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(convertedDate); 
 Ssortingng 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()); 

Essaye ça

Ce code vous aidera à faire un résultat comme le 17 février 20h49.

  Ssortingng 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"); Ssortingng newDateStr = postFormater.format(date).toUpperCase(); Ssortingng newTimeStr = sdf.format(time); System.out.println("Date : "+newDateStr); System.out.println("Time : "+newTimeStr); } catch (Exception e) { e.printStackTrace(); } 

Résultat :

Date: 17 février

Heure: 20h49

 SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); Ssortingng dateInSsortingng = "07/06/2013"; try { Date date = formatter.parse(dateInSsortingng); System.out.println(date); System.out.println(formatter.format(date)); } catch (ParseException e) { e.printStackTrace(); } 

Sortie:

 2014/08/06 16:06:54 2014/08/06 16:06:54 
 GregorianCalendar date; CharSequence dateForMart = android.text.format.DateFormat.format("yyyy-MM-dd", date); Toast.makeText(LogmeanActivity.this,dateForMart,Toast.LENGTH_LONG).show();