首页 技术 正文
技术 2022年11月14日
0 收藏 834 点赞 3,026 浏览 5335 个字

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中Date 有一些不建议用的方法

Java中时间格式处理,指定N天/小时等之后的时间

4)运用已有的一些jar依赖,比如

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

其中已经集成了的api

import org.apache.commons.lang3.time.DateUtils;import java.text.SimpleDateFormat;
import java.util.Date;public class TestLongtime { public static void main(String[] args) { SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sd.format(new Date()));
System.out.println(sd.format(new Date().getTime())); Date now = new Date();
Date year = DateUtils.addYears(now, -2);
Date day = DateUtils.addDays(now, -2);
Date hour = DateUtils.addHours(now, -2);
Date minute = DateUtils.addMinutes(now, -2);
Date second = DateUtils.addSeconds(now, -2); //long bb = Long.parseLong(startDate.toString()); System.out.println(sd.format(year));
System.out.println(sd.format(day));
System.out.println(sd.format(hour));
System.out.println(sd.format(minute));
System.out.println(sd.format(second)); }
}

  

5)常用的String类型转换到long类型

long bb = Long.parseLong(startDate.toString());

6)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); }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,985
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,501
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,345
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,128
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,763
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,840