我正在尝试将字符串格式的日期与当前日期进行比较。这就是我的做法(尚未测试,但应该可以工作),但是我使用的是不赞成使用的方法。有什么好的建议吗?谢谢。

PS:我真的很讨厌用Java做Date的事情。做同一件事的方法有很多,以至于您真的不确定哪一种是正确的,因此我在这里提出问题。

String valid_until = "1/1/1990";

Calendar cal = Calendar.getInstance();

SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");

Date strDate = sdf.parse(valid_until);

int year = strDate.getYear(); // this is deprecated

int month = strDate.getMonth() // this is deprecated

int day = strDate.getDay(); // this is deprecated

Calendar validDate = Calendar.getInstance();

validDate.set(year, month, day);

Calendar currentDate = Calendar.getInstance();

if (currentDate.after(validDate)) {

catalog_outdated = 1;

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐