日期比较 android

Comparison of dates android

我有什么

我有一个服务器日期 w3c date format 2016-02-13T09:53:49.871Z 通知

我想要的

我想将 w3c format 转换为 device time zone 然后从设备获取当前日期以检查 sever date 是否等于 设备日期

我的问题

我在 Date formattedServerDeviceDate=new Date(serverDateDeviceFormat);

得到 Parse error: 2016-03-10 15:45:36

我的代码

public boolean isTodaysNotification(String serverDate)throws Exception{
        boolean isTodaysNotification=false;
        SimpleDateFormat simpleDateFormatW3C = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
        simpleDateFormatW3C.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date dateServer = simpleDateFormatW3C.parse(serverDate);

        TimeZone deviceTimeZone = TimeZone.getDefault();
        SimpleDateFormat simpleDeviceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDeviceFormat.setTimeZone(deviceTimeZone);
        String serverDateDeviceFormat = simpleDeviceFormat.format(dateServer);

        Date formattedServerDeviceDate=new Date(serverDateDeviceFormat); // formatted to device time zone (w3c to utc)


        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd"); // formatting  to consider only  "yyyy-MM-dd"
        String strServerDate=simpleFormat.format(formattedServerDeviceDate); // server date
        String strTodaysDate=simpleFormat.format(new Date()); // current date

        if (new Date(strTodaysDate).compareTo(new Date(strServerDate))==0){
            isTodaysNotification=true;
        }
        else {
            isTodaysNotification=false;
        }
        return  isTodaysNotification;
    }

试试这个方法:使用 simpleDeviceFormat.parse(...) 而不是 new Date(String)

从字符串到日期

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
try {  
    Date formattedServerDeviceDate = format.parse(serverDateDeviceFormat);  
    System.out.println(formattedServerDeviceDate);  
} catch (ParseException e) {  
    e.printStackTrace();  
}

java.util.Date 的基于字符串的构造函数需要一个由方法 toString() 生成的格式的字符串。类似于 "Sat, 12 Aug 1995 13:30:00 GMT+0430".

另请参阅相关 parser 的描述。建议:不要使用已弃用的内容。

最重要的: 您无法获得 java.util.Date 的格式化实例。 此对象不携带任何格式信息,而只是一个长值的包装器。所以你尝试 "format" 日期对象是错误的。继续使用 dateServer 即可。它在全球范围内都是相同的值。

如何帮助?

当然,您可以将 Date 对象转换为 Calendar 对象,并询问年、月、日。示例:

GregorianCalendar gcal = new GregorianCalendar(TimeZone.getDefault());
java.util.Date serverDate = new Date(); // here for demonstration, use that from server
gcal.setTime(serverDate);
int year = gcal.get(Calendar.YEAR);
int month = gcal.get(Calendar.MONTH) + 1;
int dayOfMonth = gcal.get(Calendar.DAY_OF_MONTH);

但一般问题从这里开始:为了进行仅日期比较,您需要知道服务器的时区以应用相同的过程并比较日期组件(年、月、日月)。即使您知道服务器的时区,这是否有意义?为什么客户应该关心服务器内部结构?

假设您需要检查在 UTC 时区表示为 String 的日期时间是否属于默认时区的当天:

public boolean isToday(String utcDatetime, String format) throws Exception {
    DateFormat sdf = new SimpleDateFormat(format);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    Calendar c1 = Calendar.getInstance();        // calendar with default TZ
    Calendar c2 = Calendar.getInstance();
    c1.setTime(sdf.parse(utcDatetime);           // input datetime
    c2.setTime(new Date());                      // current datetime
    return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)
        && c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR);
}