Java 将字符串日期与今天的日期进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24816853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:00:29  来源:igfitidea点击:

Comparing string date with today's date

javaandroiddatedatetimecompare

提问by sok

So I have a string which is "2014-06-30 15:27" and if it is today's date it should only return "15:27" else "30/06/2014". I've already tried simpleDateFormat.parse but it didn't work very well.

所以我有一个字符串是“2014-06-30 15:27”,如果是今天的日期,它应该只返回“15:27”,否则返回“30/06/2014”。我已经尝试过 simpleDateFormat.parse 但效果不佳。

holder.data.setText(mensagem.getDate());

采纳答案by fipple

    final String stringDate = "2014-07-17 23:59";

    SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    Date date = inputFormat.parse(stringDate);

    Calendar calendarDate = Calendar.getInstance();
    calendarDate.setTime(date);

    Calendar midnight = Calendar.getInstance();
    midnight.set(Calendar.HOUR_OF_DAY, 0);
    midnight.set(Calendar.MINUTE, 0);
    midnight.set(Calendar.SECOND, 0);
    midnight.set(Calendar.MILLISECOND, 0);

    if (calendarDate.compareTo(midnight) >= 0)
    {
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        System.out.println(timeFormat.format(date));
    }
    else
    {
        SimpleDateFormat dateTimeForm = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        System.out.println(dateTimeForm.format(date));
    }

回答by Basil Bourque

LocalDateTime

LocalDateTime

First parse the string as a LocalDateTime. Replace the SPACE in the middle with Tto comply with standard ISO 8601 format. The java.time classes support ISO 8601 formats by default.

首先将字符串解析为LocalDateTime. 将中间的 SPACE 替换T为符合标准 ISO 8601 格式。java.time 类默认支持 ISO 8601 格式。

LocalDateTime ldt = LocalDateTime.parse ( "2014-06-30 15:27".replace ( " " , "T" ) );

ldt.toString(): 2014-06-30T15:27

ldt.toString(): 2014-06-30T15:27

Such a value has no real meaning. The input string lacked any clue about offset-from-UTC or time zone. So we do not know if this is 3 PM in Auckland NZ or 3 PM in Québec Canada, two very different moments. You should to assign the offset or time zone indicated by your business situation. Search Stack Overflow for how to do this, focussing on classes OffsetDateTimeand ZonedDateTime. I'll skip over this crucial issue for now.

这样的值没有实际意义。输入字符串缺乏关于 UTC 偏移量或时区的任何线索。所以我们不知道这是新西兰奥克兰的下午 3 点还是加拿大魁北克的下午 3 点,这是两个截然不同的时刻。您应该根据您的业务情况指定偏移量或时区。搜索 Stack Overflow 以了解如何执行此操作,重点关注类OffsetDateTimeZonedDateTime. 现在我将跳过这个关键问题。

LocalDate

LocalDate

Extract a date-only value. The LocalDateclass represents a date-only value without time-of-day and without time zone.

提取仅限日期的值。该LocalDate级表示没有时间一天和不同时区的日期,唯一的价值。

LocalDate ld = ldt.toLocalDate();

Today

今天

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris Franceis a new day while still “yesterday” in Montréal Québec.

时区对于确定日期至关重要。对于任何给定时刻,日期因地区而异。例如,在法国巴黎午夜过后几分钟是新的一天,而在魁北克蒙特利尔仍然是“昨天” 。

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

Comparison

比较

You can compare LocalDateobjects by calling methods such as compareTo, equals, isEqual, isBefore, isAfter.

您可以LocalDate通过调用诸如compareToequalsisEqualisBefore、 之类的方法来比较对象isAfter

if( today.isEqual( ld ) ) {
    return ldt.toLocalTime();
} else {
    return ld;
}


About java.time

关于java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*classes.

您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*

Where to obtain the java.time classes?

从哪里获得 java.time 类?

The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多

回答by Howard J

Simply use the Date class...

只需使用 Date 类...

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date exitdate = df.parse("2019-01-17");
Date currdate = new Date();
long diff = currdate.getTime() - exitdate.getTime();
long days = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);

if(days==0)
{
    System.out.println("IS TRUE"+currdate.toString());
    return true;
}