Comparing string date with today's date

ぐ巨炮叔叔 提交于 2019-12-11 13:20:04

问题


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.

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

回答1:


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



回答2:


LocalDateTime

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

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

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 OffsetDateTime and ZonedDateTime. I'll skip over this crucial issue for now.

LocalDate

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

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 France is 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 LocalDate objects by calling methods such as compareTo, equals, isEqual, isBefore, isAfter.

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

About java.time

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

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

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

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

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project 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.




回答3:


Simply use the Date class...

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


来源:https://stackoverflow.com/questions/24816853/comparing-string-date-with-todays-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!