js中时间有时分秒往后推7天
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js中时间有时分秒往后推7天相关的知识,希望对你有一定的参考价值。
参考技术A Date curDate = new Date();var myDate = new Date(curDate.getTime() + 24*7*60*60*1000); //往后推7天
Java中时间格式处理,指定N天/小时等之后的时间
1)根据当前时间,获取具体的时刻的时间
N天前
M小时之前
可用
new Date().getTime() - 24 * 60 * 60 * 1000*N【N天之前】
的方法来获取处理时间之后的具体的值,
最终转化为想要的时间格式
import java.text.SimpleDateFormat; import java.util.Date; public class getTime public static void main(String[] args) SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date time = new Date(new Date().getTime() - 24 * 60 * 60 * 1000); System.out.println(sd.format(time)); System.out.println(sd.format(new Date())); //想获得N天之前或M天之后的时间 int NDay = 5; int MDay = -10; //之后就传负数 Date timeN = new Date(new Date().getTime() - 24 * 60 * 60 * 1000 * NDay); Date timeM = new Date(new Date().getTime() - 24 * 60 * 60 * 1000 * MDay); System.out.println(sd.format(timeN)); System.out.println(sd.format(timeM)); //想获得N小时之前或M小时之后的时间 int NHour = 5; int MHour = -10; //之后就传负数 Date timeNHour = new Date(new Date().getTime() - 60 * 60 * 1000 * NHour); Date timeMHour = new Date(new Date().getTime() - 60 * 60 * 1000 * MHour); System.out.println(sd.format(timeNHour)); System.out.println(sd.format(timeMHour)); //想获得N分钟之前或M分钟之后的时间 int NMinute = 5; int MMinute = -10; //之后就传负数 Date timeNMinute = new Date(new Date().getTime() - 60 * 1000 * NMinute); Date timeMMiute = new Date(new Date().getTime() - 60 * 1000 * MMinute); System.out.println(sd.format(timeNMinute)); System.out.println(sd.format(timeMMiute)); //想获得N秒之前或M秒之后的时间 int NSecond = 5; int MSecond = -10; //之后就传负数 Date timeNSecond = new Date(new Date().getTime() - 1000 * NSecond); Date timeMSecond = new Date(new Date().getTime() - 1000 * MSecond); System.out.println(sd.format(timeNSecond)); System.out.println(sd.format(timeMSecond)); long Ntime = getDetailtime(3, 20, 1, 1); System.out.println(sd.format(Ntime)); System.out.println(Ntime); Date Ntime2 = getDetailDate(3, 20, 1, 1); System.out.println(sd.format(Ntime2)); System.out.println(Ntime2); public static Date getDetailDate(int Day, int Hour, int Minute, int Second) Date timeN = new Date(new Date().getTime() - Day * 24 * 60 * 60 * 1000 - Hour * 60 * 60 * 1000 - Minute * 60 * 1000 - Second * 1000); return timeN; public static long getDetailtime(int Day, int Hour, int Minute, int Second) long timeN = new Date().getTime() - Day * 24 * 60 * 60 * 1000 - Hour * 60 * 60 * 1000 - Minute * 60 * 1000 - Second * 1000; return timeN;
那么想要更加具体的时间,比如让用户输入具体的天,时,分钟,秒等,可以获取更加精准的想要的时刻
public static Date getDetailDate(int Day, int Hour, int Minute, int Second) Date timeN = new Date(new Date().getTime() - Day * 24 * 60 * 60 * 1000 - Hour * 60 * 60 * 1000 - Minute * 60 * 1000 - Second * 1000); return timeN; public static long getDetailtime(int Day, int Hour, int Minute, int Second) long timeN = new Date().getTime() - Day * 24 * 60 * 60 * 1000 - Hour * 60 * 60 * 1000 - Minute * 60 * 1000 - Second * 1000; return timeN;
2) 根据
java.util.Calendar中的操作
ca.add(Calendar.DATE, -N);【减去N天】
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class getTime2 public static void main(String[] args) SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); int N=2; Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 ca.setTime(new Date()); //设置时间为当前时间 ca.add(Calendar.DATE, -N); long timeDay= ca.getTime().getTime(); Calendar ca2 = Calendar.getInstance();//得到一个Calendar的实例 ca2.setTime(new Date()); //设置时间为当前时间 ca2.add(Calendar.HOUR, -N); long timeDay2= ca2.getTime().getTime(); Calendar ca3 = Calendar.getInstance();//得到一个Calendar的实例 ca3.setTime(new Date()); //设置时间为当前时间 ca3.add(Calendar.MINUTE, -N); long timeDay3= ca3.getTime().getTime(); Calendar ca4 = Calendar.getInstance();//得到一个Calendar的实例 ca4.setTime(new Date()); //设置时间为当前时间 ca4.add(Calendar.SECOND, -N); long timeDay4= ca4.getTime().getTime(); System.out.println(sd.format(timeDay)); System.out.println(sd.format(timeDay2)); System.out.println(sd.format(timeDay3)); System.out.println(sd.format(timeDay4)); Calendar ca5 = Calendar.getInstance();//得到一个Calendar的实例 ca5.setTime(new Date()); //设置时间为当前时间 ca5.add(Calendar.MONTH, -N); long timeDay5= ca5.getTime().getTime(); System.out.println(sd.format(timeDay5)); Calendar ca6 = Calendar.getInstance();//得到一个Calendar的实例 ca6.setTime(new Date()); //设置时间为当前时间 ca6.add(Calendar.YEAR, -N); long timeDay6= ca6.getTime().getTime(); System.out.println(sd.format(timeDay6));
3)Java中long类型的10位和13位的时间戳,转换为可供查看的时间格式
如果接口文档中规定的是String类型,要注意一定不能含有l,只能含有数字
public static String timestamp2Date(String str_num) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (str_num.length() == 13) String date = sdf.format(new Date(toLong(str_num))); System.out.println("将13位时间戳:" + str_num + "转化为字符串:" + date); return date; else String date = sdf.format(new Date(toLong(str_num) * 1000)); System.out.println("将10位时间戳:" + str_num + "转化为字符串:" + date); return date; public static long toLong(String obj) return Long.parseLong(obj); @Test public void Te() String aaa = "1566802997242"; String bbb = "1566801239"; System.out.println(aaa.length()); System.out.println(bbb.length()); timestamp2Date(aaa); timestamp2Date(bbb);
以上是关于js中时间有时分秒往后推7天的主要内容,如果未能解决你的问题,请参考以下文章