Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java如何进行日历比较_Java_Date - Fatal编程技术网

Java如何进行日历比较

Java如何进行日历比较,java,date,Java,Date,我从一个文本中提取开始日期,并存储在一个字符串变量中。我想将该开始日期与当前日期进行比较,即开始日期是否早于当前日期 public static void main(String[] rags) throws ParseException{ String total= "I am Going to join in scholl at 21/10/2108"; String[] effectiveDateText=total.split(" "); String effec

我从一个文本中提取开始日期,并存储在一个字符串变量中。我想将该开始日期与当前日期进行比较,即开始日期是否早于当前日期

public static void main(String[] rags) throws ParseException{
    String total= "I am Going to join in scholl at 21/10/2108";
    String[] effectiveDateText=total.split(" ");
    String effectiveDate=effectiveDateText[effectiveDateText.length-1];
    System.out.println(effectiveDate);
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Calendar today = Calendar.getInstance();
    String todate=sdf.format(today.getTime());
    System.out.println(todate);
    if(effectiveDate<todate){
        System.out.println("effective date is less then the previous date");
    }
publicstaticvoidmain(String[]rags)抛出异常{
String total=“我将于2108年10月21日加入学校”;
字符串[]effectiveDateText=total.split(“”);
字符串effectiveDate=effectiveDateText[effectiveDateText.length-1];
系统输出打印项次(生效日期);
SimpleDataFormat sdf=新的SimpleDataFormat(“日/月/年”);
Calendar today=Calendar.getInstance();
字符串todate=sdf.format(today.getTime());
系统输出打印项次(todate);

如果(effectiveDate您应该将日期解析为日期格式,并使用提供的方法,如下所示:

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

    Date date1 = sdf.parse("21/10/2108");
    Date date2 = sdf.parse("20/01/2018");

    System.out.println("date1 : " + sdf.format(date1));
    System.out.println("date2 : " + sdf.format(date2));

    // after
    if (date1.after(date2)) {

    }
    // before
    if (date1.before(date2)) {

    }
很高兴知道: 如果您想知道如何使用date1.equals(date2),您应该非常小心,因为这同样适用于毫秒精度,因此如果您允许用户在将来输入时间值,则必须使用日期增量。

Java有非常有用的方法来比较两个瞬间,即和。请参见此示例:

String total = "I am Going to join in scholl at 21/10/2018";
String[] effectiveDateText = total.split(" ");
String effectiveDate = effectiveDateText[effectiveDateText.length - 1];
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Instant joinDate = sdf.parse(effectiveDate).toInstant();
if (Instant.now().isAfter(joinDate)) {
    // ...
}
然而,你应该考虑时区。例如,目前(<代码>即时(现在)/代码> >),世界上大部分地区,12/10/2018,在某些时候,已经是13/10/2018(萨摩亚),而另一些则是11/10/2018(美国小岛,只剩下一分钟)。
顺便说一句,我将2108年10月21日改为2018年10月21日。

您可以使用此代码进行比较

LocalDate currentDate = LocalDateTime.now().toLocalDate();
String cDate = ""+currentTime.getMonth().toString()+"/"+currentTime.getDayOfMonth().toString()+"/"+currentTime.getYear().toString();
现在cDate将具有dd/MM/yyyy格式的日期字符串。 因此,对于comaparison,您可以使用Date类

Date dOne = new SimpleDateFormat("dd/MM/yyyy").parse(cDate);
Date dTwo = new SimpleDateFormat("dd/MM/yyyy").parse(effectiveDate);
然后对这两个日期使用compareTo()方法

dOne.compareTo(dTwo); // check value of this method
如果两个日期相同,则返回0,
如果小于0表示日期早于参数日期,
如果大于0,则表示日期在参数日期之后。

tl;dr 使用现代课堂

2108-10-21

分线 首先把绳子分成几段

String input = "I am Going to join in scholl at 21/10/2108";
String[] parts = input.split( " " );
看看那些部分

for ( String part : parts ) {
    System.out.println( part );
}

上午

加入

斯科尔

2108年10月21日

摘录最后一部分

String part = parts[ parts.length - 1 ]; // Subtract 1 for index (annoying zero-based counting).
LocalDate
现代方法使用java.time类。具体地说,
LocalDate
表示只包含日期的值,不包含一天中的时间,也不包含时区或UTC偏移量

将最后一部分解析为
LocalDate
。定义要匹配的格式模式

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
LocalDate ld = LocalDate.parse( part , f );
ISO 8601 只要可能,不要使用用于向人类演示的格式以文本形式交换日期时间值

相反,使用标准中为数据交换目的定义的格式。对于仅限日期的值,应为:YYYY-MM-DD

在解析/生成文本时,java.time类默认使用ISO 8601格式

String output = LocalDate.now().toString()
2018-01-23


关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,如,&

该项目现已启动,建议迁移到类

要了解更多信息,请参阅.和搜索堆栈溢出以获取许多示例和解释。规范为

您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
java.sql.*
classes

从哪里获得java.time类

  • 、和更高版本-标准Java API的一部分,带有捆绑实现。
    • Java9添加了一些次要功能和修复
    • 大多数java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类

    • 对于早期的Android(将它们解析为java.util.Date,然后使用.compareTo()。可能的重复与Selenium.FYI无关。现在,非常麻烦的旧日期时间类,如和
      java.text.SimpleDateFormat
      ,被java 8和更高版本中内置的类所取代。请参见.FYI,非常麻烦的旧日期时间类,如和
      java.text.SimpleDateFormat
      ,是now,被Java 8和更高版本中内置的类所取代。请参阅。
      Instant
      在这里不是正确的类。问题涉及仅日期值(没有时间,没有时区),但是,
      Instant
      是一个时刻,一个日期和UTC中的时间。因此不具有适当的代表性。相反,使用
      LocalDate
      。当您可以使用java.time时——这是一个非常好的主意——将过时且麻烦的
      SimpleDateFormat
      和朋友混合在一起只会使事情过于复杂。为什么要混合使用现代的java.time类与糟糕的遗留类?从JSR 310开始,遗留类完全被取代,并且永远不需要使用。@BasilBourque是对的,这真的是太复杂了。我投票赞成,概念解释得很好,复杂性也降低了
      String output = LocalDate.now().toString()