时间戳:2022-11-30 12:00:00.0,时间戳的getTime方法得到的一个long类型用来计算
yyyy-MM-dd HH:mm:ss HH大写时为24小时,hh小写时为12小时
2.对数据库中查到的时间(String类型)转化并进行比较:
前言
在写打卡的业务时,经常碰到时间戳与时间,日期与时间之间的转换以及计算,所以写一篇文章记录一下,它们之间如何转换,方便以后查阅。
一、java中对时间的处理
1.获取当前时间:
通过Time包下的LocalDate获取年月日
LocalDate localDate = LocalDate.now();//当前的日期时间
int year=localDate.getYear();// 2022
int month=localDate.getMonthValue();// 11
int day=localDate.getDayOfMonth();// 30
int week = localDate.getDayOfWeek().getValue();//当前星期几:1,2,3,4,5,6,7
通过Util包中的Date获取
Date date = new Date();
//根据时间格式设置拿到想要的时间格式
SimpleDateFormat format1= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format3= new SimpleDateFormat("HH:mm:ss");
format1.format(date);//当前时间,格式为:yyyy-MM-dd HH:mm:ss
format2.format(date);//当前时间,格式为:yyyy-MM-dd
format3.format(date);//当前时间,格式为:HH:mm:ss
通过Util包的Calendar 获取时间,分别获取年月日时分秒
Calendar cal=Calendar.getInstance();
int y=cal.get(Calendar.YEAR);
int m=cal.get(Calendar.MONTH);
int d=cal.get(Calendar.DATE);
int h=cal.get(Calendar.HOUR_OF_DAY);
int mi=cal.get(Calendar.MINUTE);
int s=cal.get(Calendar.SECOND);
//当前星期几:星期日为1,星期一为2...; 为了与数据库对应整体减一,星期天为0,其他正常
int settingWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
2.对数据库中查到的时间(String类型)转化并进行比较:
这里定义一个变量temp(yyyy-MM-dd),用来表示日期时间的前缀(当前时间-->2022-11-30),方便下面字符串的拼接使用
String temp=localDate.getYear() + "-" + localDate.getMonthValue() + "-" + localDate.getDayOfMonth() + " ";//获取当前时间组合成字符串"yyyy-MM-dd"
/*或使用Calendar
String temp=Calendar.getInstance().get(Calendar.YEAR) + "-" + Calendar.getInstance().get(Calendar.MONTH) + "-" + Calendar.getInstance().get(Calendar.DATE) + " ";*/
String类型的“yyyy-MM-dd HH:mm:ss”时间转化为“HH:mm:ss”
String time = "2022-11-30 12:00:00";
//格式化时间
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
//日期时间"2022-11-30 12:00:00"转化为时间戳2022-11-30 12:00:00.0
Timestamp timestamp = Timestamp.valueOf(time);
String s = sdf.format(new Date(timestamp.getTime()));//HH:mm:ss 12:00:00
String类型的“HH:mm:ss”时间转化为“yyyy-MM-dd HH:mm:ss”(用另一种方式)
String time = "12:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d=sdf.parse(temp+time)//结果为Date类型 Thu Jan 01 12:00:00 CST 1970
String s = sdf.format(sdf.format(d));//2022-11-16 12:00:00
3.对时间(String类型)进行运算:
先将不同类型统一转化成String类型(转化过程参考上面),然后再转化成格式为yyyy-MM-dd HH:mm:ss:s的时间戳进行运算
String time1 = "2022-11-30 12:00:00";
String time2 = "2022-11-30 13:00:00";
/**
* 日期时间"2022-11-30 12:00:00"转化为时间戳2022-11-16 12:00:00.0
*/
Timestamp timestamp1 = Timestamp.valueOf(time1);
Timestamp timestamp2 = Timestamp.valueOf(time2);
//用得到long类型的值进行计算
long l1 = timestamp1.getTime();
long l2 = timestamp2.getTime();
(l1-l2) / (1000 * 60);// 输出 60 (分钟)
二、数据库中对时间的处理
1.查看昨天是星期几
代码如下(示例):
DayOfWeek()的得到当天的星期:星期天为1,星期一为2......;减一后星期天为0,其他对应星期几就为多少(星期一为1,星期二为2......)
select *
from setting
where setting_week = DayOfWeek(date_sub(now(), interval 1 day)) - 1
2.查看用户当天的所有记录
select *
from punch
where TO_DAYS(punch_time) = TO_DAYS(now())
and punch_user_id = #{userId}