亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

java比較時(shí)間
伊謝爾倫
伊謝爾倫 2017-04-18 10:53:56
0
5
701

String date1 = “2017-04-06”;
String start = "2017-04";
String end = "2017-06";
java 計(jì)算data1是否在start和end之間。start和end即表示的是四月到六月

伊謝爾倫
伊謝爾倫

小伙看你根骨奇佳,潛力無(wú)限,來(lái)學(xué)PHP伐。

reply all(5)
PHPzhong

If you are not using Java8:


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {

    public static void main(String[] args) throws ParseException {
        String date1 = "2017-06-06";
        String start = "2017-04";
        String end = "2017-06";

        Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(date1);
        Date dStart = new SimpleDateFormat("yyyy-MM").parse(start);
        Calendar c = Calendar.getInstance();
        c.setTime(new SimpleDateFormat("yyyy-MM").parse(end));
        c.add(Calendar.MONTH, 1);
        Date dEnd = c.getTime();

        if (d1.after(dStart) && d1.before(dEnd)) {
            System.out.println("true");
        } else {
            System.out.println("false");
        }

    }

}
巴扎黑

I see someone has already mentioned Java8 before...but this way of writing is a bit awkward...Everyone has basically the same idea...

  1. The start time given should be changed to the first day of the current month

  2. The end time given should be changed to the last day of the current month

The key is how to change... There are ready-made APIs that can be called in Java 8... There is no need for parse... It will be okay if it becomes the first day, but there will be many judgments if it becomes the last day. ..

The code is as follows. According to the given conditions, an isBetween method is written

public static boolean isBetween(String date, String start, String end){
        // 把start轉(zhuǎn)化為start所在月份的第一天
        LocalDate startDate = LocalDate.now().with(YearMonth.parse(start)).with(TemporalAdjusters.firstDayOfMonth());

        // 把end轉(zhuǎn)化為end所在月份的最后一天
        LocalDate endDate = LocalDate.now().with(YearMonth.parse(end)).with(TemporalAdjusters.lastDayOfMonth());

        // 把date轉(zhuǎn)化為L(zhǎng)ocalDate
        LocalDate currentDate = LocalDate.parse(date);

        return currentDate.isAfter(startDate) && currentDate.isBefore(endDate);
    }

A brief explanation...
yyyy-mm This form of year and month has been processed by a new class in Java8. This is the with method of the interface. This involves the design of the new time API of Java8 and the signature of the with method. As follows YearMonth(它是一個(gè)TemporalAdjuster的實(shí)現(xiàn)類),根據(jù)LocalDate(它是一個(gè)Temporal的實(shí)現(xiàn)類)的with方法,其實(shí)是Temporal

means: an object to adjust Temporal對(duì)象可以根據(jù)一個(gè)TemporalAdjuster

So combined with the code written above, the idea is as follows

  1. I randomly took the current time

    ALocalDate

  2. Modify A according to the incoming

    (call the with method), that is, adjust the year and month of the time to get time BYearMonth

  3. Put B according to an adjuster

    ), adjust the day of the time, and get the final timeTemporalAdjusterTemporalAdjusters.firstDayOfMonth

In fact, many commonly used time adjustments have been encapsulated in

... It is also simple, straightforward and easy to use. You can pay attention to this classTemporalAdjusters

Finally, I provide some relationships between the Java 8 new time API that I simply connected before. Following these relationships, you can look at the specific classes and relationships. You will find that the new time API is not only easy to use but also very powerful.

左手右手慢動(dòng)作

If you are using Java8:

import java.time.LocalDate;

public class What {

    public static void main(String[] args) throws Exception {
        String date1 = "2017-04-06";
        String start = "2017-04";
        String end = "2017-06";

        LocalDate date = LocalDate.parse(date1);

        LocalDate startDate = LocalDate.parse(start + "-01");
        LocalDate endDate = LocalDate.parse(end + "-01");
        
        endDate = endDate.plusDays(endDate.getMonth().maxLength());
        
        if (date.isAfter(startDate) && date.isBefore(endDate)) {
            System.out.println("data1 在 start 和 end 之間");
        } else {
            System.out.println("data1 不在 start 和 end 之間");
        }
    }
    
}
小葫蘆

If you are not using Java8 but are using Joda-time:

  public boolean between(String date){
    String start = "2017-04";
    String end = "2017-06";
    try {
        return DateTime.parse(date).isAfter(DateTime.parse(start).withDayOfMonth(1).toInstant())
            && DateTime.parse(date).isBefore(DateTime.parse(end).plusMonths(1).withDayOfMonth(1).minusDays(1).toInstant());
    }catch (Exception e){
        return false;
    }
}
洪濤
  1. end should be converted to the last day of this month, for example 2017-06-30;

  2. If the format is determined to be yyyy-mmyyyy-mm-dd,那么用date.compareTo(start) >= 0 && date.compareTo(end) < 0, that’s it;

  3. If there is a non-standard format, such as 2017-4-6, either convert it to a standard format, or convert it to Date or long (Date is essentially long), and then compare.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template