DateTime从PHP到Java(Android):如何将字符串转换为DateTime对象?
在PHP中,将字符串转换为DateTime()非常简单:DateTime从PHP到Java(Android):如何将字符串转换为DateTime对象?,java,android,datetime,Java,Android,Datetime,在PHP中,将字符串转换为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("
$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();
这个崩溃应用程序。AndroidStudio告诉我Date(java.lang.String)已被弃用
有人能帮我吗?试试这个
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());
是的,从JDK版本1.1开始,
Date(java.lang.String)
已被弃用,并被DateFormat.parse(String s)
取代
如果必须处理不在默认区域设置中的时区,可以使用SimpleDateFromat.setTimeZone()
让解析器/格式化程序考虑时区。这样解析:
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日期选择器示例
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();
}