2013-11-26 77 views
0

在PHP中的字符串日期时间(转换)其本身非常简单:从PHP到Java(Android)的DateTime:如何将字符串转换为DateTime对象?

$dateTime = new DateTime("2013-12-11 10:109:08"); 
echo $dateTime->format("d/m/Y"); // output 11/12/2013 

什么是Java中的等价物?我在stackoverflow中看到很多问题。我找不到解决这个问题的方法。

我的最后一次尝试是:

SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.ITALIAN); 
return dateFormat.format(new Date(datetime)).toString(); 

这个崩溃的应用程序。 Android Studio告诉我Date(java.lang.String)已被弃用。

有人可以帮助我吗?

回答

2
// First convert the String to a Date 
String dateTime = "2013-11-12 13:14:15"; 
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.ITALIAN); 
Date date = dateParser.parse(dateTime); 
// Then convert the Date to a String, formatted as you dd/MM/yyyy 
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy"); 
System.out.println(dateFormatter.format(date)); 

你可以让解析器/格式化走时区考虑使用SimpleDateFromat.setTimeZone(),如果你要处理的是不是在你的默认语言环境的时区。

0

试试这个

String time1=""; 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS",Locale.US); 
          GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("US/Central")); 
          calendar.setTimeInMillis(yourmilliseconds); 
           time1=sdf.format(calendar.getTime()); 
0

是从JDK 1.1版,Date(java.lang.String)已被废弃,由DateFormat.parse(String s)取代。

0

解析它像:

SimpleDateFormat formatter = 
      new SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY); 

     Calendar date = Calendar.getInstance(); 
     try { 
      date.setTime(formatter.parse("12.12.2010")); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

看一看我的Android日期选取例如here

相关问题