在Android中比较date的最佳方法

我正在尝试将string格式的date与当前date进行比较。 这是我做的(没有testing,但应该工作),但我使用不推荐的方法。 任何好的build议替代? 谢谢。

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

你的代码可以减less到

 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date strDate = sdf.parse(valid_until); if (new Date().after(strDate)) { catalog_outdated = 1; } 

要么

 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date strDate = sdf.parse(valid_until); if (System.currentTimeMillis() > strDate.getTime()) { catalog_outdated = 1; } 

您可以直接从Date创buildCalendar

 Calendar validDate = new GregorianCalendar(); validDate.setTime(strDate); if (Calendar.getInstance().after(validDate)) { catalog_outdated = 1; } 

请注意,在代码工作之前,正确的格式是(“dd / MM / yyyy”)。 “mm”意思是minuts!

 String valid_until = "01/07/2013"; SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date strDate = null; try { strDate = sdf.parse(valid_until); } catch (ParseException e) { e.printStackTrace(); } if (new Date().after(strDate)) { catalog_outdated = 1; } 

你可以使用compareTo()

如果当前对象小于其他对象,CompareTo方法必须返回负数,如果当前对象大于其他对象,则为正数;如果两个对象相等,则返回零。

 // Get Current Date Time Calendar c = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm aa"); String getCurrentDateTime = sdf.format(c.getTime()); String getMyTime="05/19/2016 09:45 PM "; Log.d("getCurrentDateTime",getCurrentDateTime); // getCurrentDateTime: 05/23/2016 18:49 PM if (getCurrentDateTime.compareTo(getMyTime) < 0) { } else { Log.d("Return","getMyTime older than getCurrentDateTime "); } 
 String date = "03/26/2012 11:00:00"; String dateafter = "03/26/2012 11:59:00"; SimpleDateFormat dateFormat = new SimpleDateFormat( "MM/dd/yyyy hh:mm:ss"); Date convertedDate = new Date(); Date convertedDate2 = new Date(); try { convertedDate = dateFormat.parse(date); convertedDate2 = dateFormat.parse(dateafter); if (convertedDate2.after(convertedDate)) { txtView.setText("true"); } else { txtView.setText("false"); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

它返回true ..你也可以在date.before和date.equal的帮助下检查before和equal。

 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault()); Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); Date date1 = dateFormat.parse("2013-01-01"); Date date2 = dateFormat.parse("2013-01-02"); calendar1.setTime(date1); calendar2.setTime(date2); System.out.println("Compare Result : " + calendar2.compareTo(calendar1)); System.out.println("Compare Result : " + calendar1.compareTo(calendar2)); 

将此日历表示的时间与给定日历表示的时间进行比较。

如果两个日历的时间相等,则返回0;如果此日历的时间在另一个之前,则返回-1;如果此日历的时间在另一个之后,则返回1。

 Calendar toDayCalendar =Calendar.getInstance(); Date date1=toDayCalendar.getTime(); Calendar tomorrowCalendar =Calendar.getInstance(); tomorrowCalendar.add(Calendar.DAY_OF_MONTH,1); Date date2=tomorrowCalendar.getTime(); //date1 is a present date and date2 is tomorrow date if(date1.compareTo(date2)<0){ //0 comes when two date are same, //1 comes when date1 is higher then date2 //-1 comes when date1 is lower then date2 } 

你可以使用validDate.setTime(strDate)看看javadoc http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html

你可以试试这个

 Calendar today = Calendar.getInstance (); today.add(Calendar.DAY_OF_YEAR, 0); today.set(Calendar.HOUR_OF_DAY, hrs); today.set(Calendar.MINUTE, mins ); today.set(Calendar.SECOND, 0); 

你可以使用today.getTime()来检索值并进行比较。

将date转换为日历并在那里进行计算。 🙂

 Calendar cal = Calendar.getInstance(); cal.setTime(date); int year = cal.get(Calendar.YEAR); int month = cal.geT(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); //same as cal.get(Calendar.DATE) 

要么:

 SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy"); Date strDate = sdf.parse(valid_until); if (strDate.after(new Date()) { catalog_outdated = 1; } 

乔达时间

java.util.Date和.Calendar类是非常麻烦的。 避免他们。 在Java 8中使用Joda-Time或新的java.time包。

LOCALDATE的

如果你想要date而不需要时间,那么使用LocalDate类。

时区

获取当前date取决于时区。 在蒙特利尔之前,巴黎的一个新的date将会翻滚。 指定所需的时区,而不是依赖于JVM的默认值。

Joda-Time的例子2.3。

 DateTimeFormat formatter = DateTimeFormat.forPattern( "d/M/yyyy" ); LocalDate localDate = formatter.parseLocalDate( "1/1/1990" ); boolean outdated = LocalDate.now( DateTimeZone.UTC ).isAfter( localDate ); 

有时我们需要用date做一个列表,比如

今天与小时

昨天与昨天

其他date23/06/2017

为了做到这一点,我们需要比较当前的时间和我们的数据。

 Public class DateUtil { Public static int getDateDayOfMonth (Date date) { Calendar calendar = Calendar.getInstance (); Calendar.setTime (date); Return calendar.get (Calendar.DAY_OF_MONTH); } Public static int getCurrentDayOfMonth () { Calendar calendar = Calendar.getInstance (); Return calendar.get (Calendar.DAY_OF_MONTH); } Public static String convertMillisSecondsToHourString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("HH: mm"); Return formatter.format (date); } Public static String convertMillisSecondsToDateString (long millisSecond) { Date date = new Date (millisSecond); Format formatter = new SimpleDateFormat ("dd / MM / yyyy"); Return formatter.format (date); } Public static long convertToMillisSecond (Date date) { Return date.getTime (); } Public static String compare (String stringData, String yesterday) { String result = ""; SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); Date date = null; Try { Date = simpleDateFormat.parse (stringData); } Catch (ParseException e) { E.printStackTrace (); } Long millisSecond = convertToMillisSecond (date); Long currencyMillisSecond = System.currentTimeMillis (); If (currencyMillisSecond> millisSecond) { Long diff = currencyMillisSecond - millisSecond; Long day = 86400000L; If (diff <day && getCurrentDayOfMonth () == getDateDayOfMonth (date)) { Result = convertMillisSecondsToHourString (millisSecond); } Else if (diff <(day * 2) && getCurrentDayOfMonth () -1 == getDateDayOfMonth (date)) { Result = yesterday; } Else { Result = convertMillisSecondsToDateString (millisSecond); } } Return result; } } 

你也可以在GitHub和这篇文章中查看这个例子。