作用:解决浮点数失真的问题
构造器和方法名称
Date
SimpleDateFormat
为什么要使用SimpleDateFormat
作用:
- 代表简单日期格式化,可以用来把日期对象、时间毫秒值格式化为我们想要的格式。
常见构造器和方法
时间格式的常见符号
将SimpleDateFormat解析字符串时间成为日期对象
package com.zz.Date;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String args[]) throws ParseException {
// 记录秒杀、xj、xp的日期
String start = "2023年11月11日 0:0:0";
String end = "2023年11月11日 0:10:0";
String xj = "2023年11月11日 0:01:18";
String xp = "2023年11月11日 0:10:57";
//将日期格式化
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date startDate = simpleDateFormat.parse(start);
Date endDate = simpleDateFormat.parse(end);
Date xjDate = simpleDateFormat.parse(xj);
Date xpDate = simpleDateFormat.parse(xp);
//将日期对象转化为毫秒比较
long startTime = startDate.getTime();
long endTime = endDate.getTime();
long xjTime = xjDate.getTime();
long xpTime = xpDate.getTime();
if(xjTime >= endTime || xjTime <= startTime){
System.out.println("小贾秒杀失败!");
}else{
System.out.println("小贾秒杀成功!");
}
if(xpTime >= endTime || xpTime <= startTime){
System.out.println("小皮秒杀失败!");
}else{
System.out.println("小皮秒杀成功!");
}
}
}
Calendar
构造器和方法名称
package com.zz.Calendar;
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String args[]){
//获得系统此时对应的日历对象
Calendar now = Calendar.getInstance();
System.out.println(now);
//获取日历中的某个信息
int year = now.get(Calendar.YEAR);
System.out.println(year);
int days = now.get(Calendar.DAY_OF_WEEK);
System.out.println(days);
//拿到日历中记录的日期对象
Date d = now.getTime();
System.out.println(d);
//修改日历中的某个信息
now.set(Calendar.MONTH, 9);//修改月份成10月份
System.out.println(now);
//位某个信息增加或者减少多少
now.add(Calendar.DAY_OF_WEEK, 30);
}
}
注意:Calendar是可变对象,一旦修改后其对象本身表示的事件将产生变化。