如何比较当日&给定日期和月份的月份Java月

问题描述:

我想将当前日期与给定日期进行比较。当前日期格式为(dd-MM-YYYY)。我不想得到今年。假设今天的日期是2016年8月30日。我只想得到30-08,我不想要当年2016。给定的日期是15-08和31-08。我没有得到像30-08这样的当前日期。我该如何获得和如何比较?这是我尝试过的代码:

I want to compare current date with the given date. Current date format will be (dd-MM-YYYY). I don't want to get the current year. Suppose today's date is 30-08-2016. I want to get only 30-08, I don't want current year 2016. The given date is 15-08 and 31-08. I did not get the current date like this 30-08. How can I get this and how can I compare? Here is my code which I have tried:

Date date1_aug, date2_aug, cur_date;

String cur_date_string, sDay, sMonth;

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-YYYY");


Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int day = calendar.get(Calendar.DATE);

sDay = Integer.toString(day);
sMonth = Integer.toString(month);

cur_date_string = sDay+"-"+sMonth;

            try {
                cur_date = sdf.parse(cur_date_string);
            } catch (java.text.ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }




  1. 另一个功能:

  1. another function:

        try{
                    date1_aug = sdf.parse("15-08");
                    date2_aug = sdf.parse("31-08");

                    if(cur_date.equals(date1_aug) || cur_date.after(date1_aug) || cur_date.before(date2_aug)){
                        textView_Amount_LateFee.setText(Config.fine_aug.get(1));
                    }


                }catch(ParseException ex){
                    ex.printStackTrace();
                }



java.time.MonthDay



ThreeTenABP 项目,它是 = nofollow noreferrer> Java时间类内置于Java 8及更高版本中。避免麻烦的旧式旧式日期时间类。

java.time.MonthDay

Use modern date-time classes in the ThreeTenABP project, an Android adaptation of the back-port of the java-time classes built into Java 8 and later. Avoid the troublesome old legacy date-time classes.

这些java.time类包括 MonthDay 类代表一个月和一个月中的某天

These java.time classes include the MonthDay class to represent a month and a day-of-month without a year.

MonthDay md = MonthDay.of( 8 , 15 );

此类已实现 可比较 接口及其 compareTo 方法。还实现了 等于 isBefore isAfter 。因此,工作就完成了。

This class already implements the Comparable interface and its compareTo method. Also implements methods equals, isBefore, and isAfter. So, job done.

确定当前 MonthDay 需要一个时区。在任何给定的时刻,日期都会根据时区

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






关于java.time



java.time 框架内置于Java 8及更高版本中。这些类取代了麻烦的旧旧版日期时间类,例如 java.util.Date Calendar ,& SimpleDateFormat


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.

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

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

要了解更多信息,请参见 Oracle教程。并在Stack Overflow中搜索许多示例和说明。规范为 JSR 310

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

从哪里获取java.time类?

Where to obtain the java.time classes?


  • Java SE 8 SE 9 和后来的


    • 内置。

    • 具有捆绑实现的标准Java API的一部分。

    • Java 9添加了一些次要功能和修复。

    • Java SE 8 and SE 9 and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.

      • 许多java.time功能都被反向移植到Java 6& nofollow noreferrer> ThreeTen-Backport

      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….