创建订单24小时后取消
离取消订单还剩的时间:
Map
java.util.Date date = (Date) result.get("createTime");
int available = 1;
java.util.Date now = new Date();
result.put("createTimeLeave",DurationFormatUtils.formatPeriod(now.getTime(), DateUtils.addDays(date, available).getTime(), "d'天'HH'小时'mm'分钟'ss'秒'"));
用了apache commons-lang
Java获取当天剩余时间的几种方法:
转自:https://blog.csdn.net/a_bang/article/details/53589206
三种获取当天剩余时间的简洁方式,分别使用DateUtils、DateTime和LocalDateTime,有时间再详解这三个类的更多方法。下面是编写的代码,有更多好的方式,欢迎留言。
import org.apache.commons.lang.time.DateUtils;import org.joda.time.DateTime;import org.joda.time.Duration;import org.junit.Test;import java.time.LocalDateTime;import java.time.temporal.ChronoUnit;import java.util.Calendar;/**
代码如下:
import org.apache.commons.lang.time.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.junit.Test;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
/**
* Created by Administrator on 2016/12/12.
*/
public class TimeTest {
@Test
public void getTime() {
//方法一
long milliSecondsLeftToday = 86400000 - DateUtils.getFragmentInMilliseconds(Calendar.getInstance(), Calendar.DATE);
long secondsLeftToday = 86400 - DateUtils.getFragmentInSeconds(Calendar.getInstance(), Calendar.DATE);
System.out.println("当天剩余毫秒1:" + milliSecondsLeftToday);
System.out.println("当天剩余秒1:" + secondsLeftToday);
//方法二
DateTime dateTime = new DateTime().millisOfDay().withMaximumValue();
long millSeconds2 = new Duration(new DateTime(), dateTime).getMillis();
long count = new Duration(new DateTime(), dateTime).getStandardSeconds();
System.out.println("当天剩余毫秒2:" + millSeconds2);
System.out.println("当天剩余秒2:" + count);
//方法三:LocalDateTime和ChronoUnit为1.8新增
LocalDateTime midnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
long millSeconds = ChronoUnit.MILLIS.between(LocalDateTime.now(),midnight);
long seconds = ChronoUnit.SECONDS.between(LocalDateTime.now(), midnight);
System.out.println("当天剩余毫秒3:" + millSeconds);
System.out.println("当天剩余秒3:" + seconds);
}
}