Home  >  Q&A  >  body text

java比较时间

String date1 = “2017-04-06”;
String start = "2017-04";
String end = "2017-06";
java 计算data1是否在start和end之间。start和end即表示的是四月到六月

伊谢尔伦伊谢尔伦2743 days ago593

reply all(5)I'll reply

  • PHPz

    PHPz2017-04-18 10:55:56

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

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:55:56

    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转化为start所在月份的第一天
            LocalDate startDate = LocalDate.now().with(YearMonth.parse(start)).with(TemporalAdjusters.firstDayOfMonth());
    
            // 把end转化为end所在月份的最后一天
            LocalDate endDate = LocalDate.now().with(YearMonth.parse(end)).with(TemporalAdjusters.lastDayOfMonth());
    
            // 把date转化为LocalDate
            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(它是一个TemporalAdjuster的实现类),根据LocalDate(它是一个Temporal的实现类)的with方法,其实是Temporal

    means: an object to adjust Temporal对象可以根据一个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.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:55:56

    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 之间");
            }
        }
        
    }
    

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:55:56

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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:55:56

    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.

    reply
    0
  • Cancelreply