2012-03-30 11 views
47

Próbuję sparsować ciąg znaków do pola daty w aplikacji dla systemu Android, ale nie mogę uzyskać poprawności. Oto ciąg, który próbuję przekonwertować na datę "03/26/2012 11:49:00 AM". Funkcja używam to:Konwertuj ciąg znaków na datę w java

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

ale wciąż otrzymuję 3/1/112 11:49 AM jako wynik .. Każda pomoc będę naprawdę wdzięczny. Dzięki

+2

Gdzie widzisz "3/1/112 11:49 AM"? Zwrócona wartość jest datą, a nie łańcuchem, więc musisz robić * coś *, aby zobaczyć wynik jako ciąg ... –

+0

Widzę Mon Mar 26 11:49:00 IST 2012 jako wynik. –

+0

spróbuj ustawić wyłuskiwanie przy użyciu parametru dateFormat.setLenient (true), a następnie sprawdź wyniki analizy składniowej. – manub

Odpowiedz

103

Mylisz się w sposób wyświetlić dane jak sądzę, bo dla mnie:

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

Wydruki:

Mon Mar 26 11:49:00 EEST 2012 
+0

wszyscy mieli rację. był to konwersja poprawnie, ale używałem Date.getYear(), getMonth() i wszystkich tych, których używałem nieprawidłowo. Dzięki za pomoc. – bflosabre91

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

Spróbuj

17

poszło OK gdy użyłem Locale.US parametr w 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); 
2

Ten kod pomoże ci uzyskać wynik podobny do 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(); 
    } 

Wynik:

: Feb 17

Czas: 20:49

0
GregorianCalendar date; 

CharSequence dateForMart = android.text.format.DateFormat.format("yyyy-MM-dd", date); 

Toast.makeText(LogmeanActivity.this,dateForMart,Toast.LENGTH_LONG).show(); 
1
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(); 
} 

wyjściowa:

2014/08/06 16:06:54 
2014/08/06 16:06:54