从纪元日期减去 5 分钟
Subtracting 5 minutes from epoch date
从给定纪元日期减去 5 分钟的最佳方法是什么?
public long fiveMinutesAgo(String epochDate) {
//ToDo
return fiveMinBack;
}
epochDate 必须是日期。使用日历:
Calendar calendar = Calendar.getInstance();
calendar.setTime(epochDate);
calendar.add(Calendar.MINUTE, -5);
Date result = calendar.getTime();
这是您方法的正文:
private static final long FIVE_MINS_IN_MILLIS = 5 * 60 * 1000;
public long fiveMinutesAgo(String epochDate) throws ParseException {
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
long time = df.parse(epochDate).getTime();
return time - FIVE_MINS_IN_MILLIS;
}
时间以自纪元以来的毫秒为单位,因此要找出五分钟之前,您只需减去以毫秒为单位的 5 分钟 (5 * 60 * 1000)。
我建议将该方法重命名为:fiveMinutesBefore()
并可能将其分为两种方法:一种用于将字符串日期解析为时间,另一种用于从时间中减去分钟数。
您也可以考虑使用 Joda-Time,因为它比标准 Java 日期包设计得更好(并且线程更安全)。
您可以从得到的日期中减去 5 分钟(相当于毫秒):-
//convert input string epochDate to Date object based on the format
long ms=date.getTime();
Date updatedDate=new Date(ms - (5 * 60000)); //60000 is 1 minute equivalent in milliseconds
return updatedDate.getTime();
您可以使用其他用户使用的任何上述方法,但如果有兴趣请尝试
Java 8 日期和时间 API
public void subtract_minutes_from_date_in_java8 ()
{
LocalDateTime newYearsDay = LocalDateTime.of(2015, Month.JANUARY, 1, 0, 0);
LocalDateTime newYearsEve = newYearsDay.minusMinutes(1);// In your case use 5 here
java.time.format.DateTimeFormatter formatter =java.time.format.DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss S");
logger.info(newYearsDay.format(formatter));
logger.info(newYearsEve.format(formatter));
}
输出:
01/01/2015 00:00:00 CST
12/31/2014 23:59:00 CST
Class LocalDateTime 是 java.time 包中存在的不可变日期时间对象 Java 8 表示日期时间,通常被视为年月日时分秒。
下面是使用的of()
方法的语法:
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)
它从年、月、日、小时和分钟中获取 LocalDateTime 的实例,将秒和纳秒设置为零。
从给定纪元日期减去 5 分钟的最佳方法是什么?
public long fiveMinutesAgo(String epochDate) {
//ToDo
return fiveMinBack;
}
epochDate 必须是日期。使用日历:
Calendar calendar = Calendar.getInstance();
calendar.setTime(epochDate);
calendar.add(Calendar.MINUTE, -5);
Date result = calendar.getTime();
这是您方法的正文:
private static final long FIVE_MINS_IN_MILLIS = 5 * 60 * 1000;
public long fiveMinutesAgo(String epochDate) throws ParseException {
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
long time = df.parse(epochDate).getTime();
return time - FIVE_MINS_IN_MILLIS;
}
时间以自纪元以来的毫秒为单位,因此要找出五分钟之前,您只需减去以毫秒为单位的 5 分钟 (5 * 60 * 1000)。
我建议将该方法重命名为:fiveMinutesBefore()
并可能将其分为两种方法:一种用于将字符串日期解析为时间,另一种用于从时间中减去分钟数。
您也可以考虑使用 Joda-Time,因为它比标准 Java 日期包设计得更好(并且线程更安全)。
您可以从得到的日期中减去 5 分钟(相当于毫秒):-
//convert input string epochDate to Date object based on the format
long ms=date.getTime();
Date updatedDate=new Date(ms - (5 * 60000)); //60000 is 1 minute equivalent in milliseconds
return updatedDate.getTime();
您可以使用其他用户使用的任何上述方法,但如果有兴趣请尝试
Java 8 日期和时间 API
public void subtract_minutes_from_date_in_java8 ()
{
LocalDateTime newYearsDay = LocalDateTime.of(2015, Month.JANUARY, 1, 0, 0);
LocalDateTime newYearsEve = newYearsDay.minusMinutes(1);// In your case use 5 here
java.time.format.DateTimeFormatter formatter =java.time.format.DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss S");
logger.info(newYearsDay.format(formatter));
logger.info(newYearsEve.format(formatter));
}
输出:
01/01/2015 00:00:00 CST
12/31/2014 23:59:00 CST
Class LocalDateTime 是 java.time 包中存在的不可变日期时间对象 Java 8 表示日期时间,通常被视为年月日时分秒。
下面是使用的of()
方法的语法:
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)
它从年、月、日、小时和分钟中获取 LocalDateTime 的实例,将秒和纳秒设置为零。