按分钟比较日历时间

时间:2018-12-20 01:49:34

标签: java java.util.date java-date

我想以“分钟”为单位计算“星期一17:00”和“星期二5:00”之间的差异 如何使用Java实现此目的。我不太了解如何使用日历,并且simpleDateFormat不支持Mon,Tue,Wed等e.t.c功能。有帮助吗?

问题仅涉及星期,因此最早的是“ SUN 00:00”,最晚的是“ SAT 23:59”。

P.S。给定许多这种格式的字符串,我还想将它们从最先发生到最后发生的顺序排序。因为我认为先对它们进行排序会简化任务(确定差异)。

3 个答案:

答案 0 :(得分:1)

可能是这样的

import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class DateCompare {

    public static void main(String[] args) {
        try {
            final String start = "Mon 17:00";
            final String end = "Tue 5:00";
            SimpleDateFormat formatter = new SimpleDateFormat("EEE HH:mm", Locale.US);
            long diffMinutes = TimeUnit.MILLISECONDS.toMinutes(formatter.parse(end).getTime() - formatter.parse(start).getTime()); 
            System.out.println(diffMinutes + " minutes");
        } catch(Exception ex) {
            ex.printStackTrace();
        }

    }

}

答案 1 :(得分:1)

Java没有代表星期几(星期几和一天中的时间)的类型。我建议:

  • 使用现代Java日期和时间API java.time。
  • 设计一个代表您的时代的课程。
  • 班级内部仅将您的时间表示为您决定的某个星期中的特定日期和时间。这样一来,您可以在几分钟之内免费获得分类和差异。

您的课程可能像这样:

df2.index=df.index
pd.concat([df,df2],axis=1)
Out[19]: 
   user_id event_type  a  b  c
0        1          a  2  1  0
1        1          a  2  1  0
2        1          b  2  1  0
3        2          a  1  1  1
4        2          b  1  1  1
5        2          c  1  1  1

现在可以对/** A time of week like "MON 17:00". In other words a day of week and time of day. */ public class TimeOfWeek implements Comparable<TimeOfWeek> { private static DateTimeFormatter dayTimeFormatter = new DateTimeFormatterBuilder() .parseCaseInsensitive() .appendPattern("EEE H:mm") .toFormatter(Locale.ENGLISH); /** First day of the week used internally for date-times, Sun Dec 28, 1969 */ private static LocalDate firstDate = LocalDate.EPOCH.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY)); /** * Internal representation; * always within the week of the epoch, Sun Dec 28, 1969 through Sat Jan 3, 1970. */ private LocalDateTime dateTime; public TimeOfWeek(String dayTimeString) { TemporalAccessor parsed = dayTimeFormatter.parse(dayTimeString); DayOfWeek dow = DayOfWeek.from(parsed); LocalTime time = LocalTime.from(parsed); dateTime = firstDate.with(TemporalAdjusters.nextOrSame(dow)).atTime(time); assert ! dateTime.isBefore(firstDate.atStartOfDay()) : dateTime; assert dateTime.isBefore(firstDate.plusWeeks(1).atStartOfDay()) : dateTime; } /** The order is by day of week, Sunday first, then by time of day. */ @Override public int compareTo(TimeOfWeek other) { return this.dateTime.compareTo(other.dateTime); } /** @return The difference in minutes between this and other (signed) */ int minutesUntil(TimeOfWeek other) { return Math.toIntExact(ChronoUnit.MINUTES.between(this.dateTime, other.dateTime)); } @Override public String toString() { return dateTime.format(dayTimeFormatter); } } 个对象的列表进行排序:

TimeOfWeek

输出:

  

[星期日0:00,星期一17:00,星期二5:00,星期六23:59]

要找到已排序列表中对象之间的成对差异:

    List<TimeOfWeek> dayTimes = Arrays.asList(new TimeOfWeek("Tue 5:00"),
            new TimeOfWeek("SAT 23:59"),
            new TimeOfWeek("SUN 00:00"),
            new TimeOfWeek("MON 17:00"));
    dayTimes.sort(Comparator.naturalOrder());
    System.out.println(dayTimes);
    for (int i = 1; i < dayTimes.size(); i++) {
        TimeOfWeek start = dayTimes.get(i - 1);
        TimeOfWeek end = dayTimes.get(i);
        System.out.println("Difference between " + start + " and " + end + ": "
                + start.minutesUntil(end) + " minutes");
    }

链接: Oracle tutorial: Date Time解释了如何使用Difference between Sun 0:00 and Mon 17:00: 2460 minutes Difference between Mon 17:00 and Tue 5:00: 720 minutes Difference between Tue 5:00 and Sat 23:59: 6899 minutes

答案 2 :(得分:0)

如果不允许使用SimpleDateFormat,则最好使用Apache lang 3。

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.time.DateUtils;

public class DateCompare {

    public static void main(String[] args) {
        try {
            final String start = "MON 17:00";
            final String end = "Tue 5:00";
            List<Date> dates = Arrays.asList(DateUtils.parseDate(start, Locale.US, "EEE HH:mm"), DateUtils.parseDate(end, Locale.US, "EEE HH:mm"));
            Collections.sort(dates);
            long diffMinutes = TimeUnit.MILLISECONDS.toMinutes(dates.get(1).getTime() - dates.get(0).getTime()); 
            System.out.println(diffMinutes + " minutes");
        } catch(Exception ex) {
            ex.printStackTrace();
        }

    }

}