gpt4 book ai didi

java - 如何在android中将字符串转换为日期

转载 作者:行者123 更新时间:2023-12-02 05:22:56 25 4
gpt4 key购买 nike

我有字符串日期时间“2014-10-13 18:22:54.71”,我想将其转换为 Date

我将使用该算法

public static boolean IsthatMorethanTwo(String DateString) {
Calendar thatDay = Calendar.getInstance();
thatDay.setTime(new Date(DateString));
Calendar today = Calendar.getInstance();
long diff = today.getTimeInMillis() - thatDay.getTimeInMillis();
long days = diff / (24 * 60 * 60 * 1000);
return days >= 2;
}

我该怎么做?

最佳答案

您可以使用SimpleDateFormat类。

像这样:

String dateString = "2014-10-13 18:22:54.71";    
SimpleDateFormat format =
new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.S");

try {
Date parsed = format.parse(dateString);
}
catch(ParseException pe) {
System.out.println("ERROR: Cannot parse \"" + dateString + "\"");
}

关于java - 如何在android中将字符串转换为日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26348401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com