// 创建一个Date对象,代表的是系统当前此刻日期时间
public class DateTest {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
}
}
结果:Mon Nov 14 20:45:21 CST 2022
public class DateTest {
public static void main(String[] args) {
// 把时间毫秒值转换为Date日期对象
Date date = new Date(111111123123L);
System.out.println(date); // Tue Jul 10 08:12:03 CST 1973
}
}
public class DateTest {
public static void main(String[] args) {
// 从1970.1.1 0点到此刻的毫秒数
Date date = new Date();
System.out.println(date.getTime()); // 1668430105116
}
}
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
// 放入时间毫秒值转换为日期对象
Date date = new Date();
date.setTime(1668430105116L);
System.out.println(date); // Mon Nov 14 20:48:25 CST 2022
}
}
代表简单日期格式化,可以用来把日期对象、时间毫秒值格式化成我们想要的形式。
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
Date date = new Date();
long time = date.getTime();
// 格式化日起对象
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String res = simpleDateFormat.format(date);
System.out.println(res);
String res2 = simpleDateFormat.format(time);
System.out.println("res2:" + res2);
}
}
结果:
// 2022-11-14 21:12:23
// res2:2022-11-14 21:12:23
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
Date date = new Date();
long time = date.getTime();
// 格式化日起对象
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEE a");
String res = simpleDateFormat.format(date);
System.out.println(res);
}
}
// 2022-11-14 21:15:18 星期一 下午
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException {
// SimpleDateFormat解析字符串时间成为日期时间
String dateStr = "2022-12-12 12:12:11";
// 格式化
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date res = simpleDateFormat.parse(dateStr);
System.out.println(res);
// Mon Dec 12 12:12:11 CST 2022
}
}
package bistu.time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException {
String start = "2023-11-11 00:00:00";
String end = "2023-11-11 00:10:00";
String xiaoJiaStr = "2023-11-11 00:01:18";
String xiaoPiStr = "2023-11-11 00:10:51";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = simpleDateFormat.parse(start);
Date endDate = simpleDateFormat.parse(end);
Date XiaoJiaDate = simpleDateFormat.parse(xiaoJiaStr);
Date xiaoPiDate = simpleDateFormat.parse(xiaoPiStr);
if ((XiaoJiaDate.after(startDate)) && (XiaoJiaDate.before(endDate))) {
System.out.println("小贾抢购成功!");
}
if ((xiaoPiDate.after(startDate)) && (xiaoPiDate.before(endDate))) {
System.out.println("小皮抢购成功!");
}
}
}
或者
Calendar
代表的是系统此时此刻时间对应的日历
通过它可以单独获取、修改时间中的年、月、日、时、分、秒等
package bistu.time;
import java.util.Calendar;
import java.util.Date;
public class CalendarTest {
public static void main(String[] args) {
// 得到系统此刻时间对应的日历对象
Calendar now = Calendar.getInstance();
System.out.println(now);
/*
java.util.GregorianCalendar[time=1668435046552,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,
transitions=29,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,
MONTH=10,WEEK_OF_YEAR=47,WEEK_OF_MONTH=3,DAY_OF_MONTH=14,DAY_OF_YEAR=318,DAY_OF_WEEK=2,
DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=10,HOUR_OF_DAY=22,MINUTE=10,SECOND=46,MILLISECOND=552,
ZONE_OFFSET=28800000,DST_OFFSET=0]
*/
// 获取日历中的某个信息
int yerar = now.get(Calendar.YEAR);
System.out.println(yerar); // 2022
int month = now.get(Calendar.MONTH) + 1;
System.out.println(month); // 11
int day = now.get(Calendar.DAY_OF_MONTH);
System.out.println(day); // 14
// 一年中的第几天
int days = now.get(Calendar.DAY_OF_YEAR);
System.out.println(days); // 318
// 拿到日历中记录的日期对象
Date d = now.getTime();
System.out.println(d); // Mon Nov 14 22:22:54 CST 2022
// 拿到此刻时间毫秒值
long time = now.getTimeInMillis();
// 修改日历中的某个信息
now.set(Calendar.YEAR,2023);
System.out.println(now);
/*
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,
dstSavings=0,useDaylight=false,transitions=29,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,
ERA=1,YEAR=2023,MONTH=10,WEEK_OF_YEAR=47,WEEK_OF_MONTH=3,DAY_OF_MONTH=14,DAY_OF_YEAR=318,DAY_OF_WEEK=2,
DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,
HOUR=10,HOUR_OF_DAY=22,MINUTE=24,SECOND=41,MILLISECOND=33,ZONE_OFFSET=28800000,DST_OFFSET=0]
*/
// 为某个信息增加或者减少多少
now.add(Calendar.DAY_OF_YEAR,100);
System.out.println(now.get(Calendar.YEAR));
}
}
// 0.获取本地日期对象
LocalDate ld = LocalDate.now();
System.out.println(ld);
// 1.获取日期对象中的信息
int year = ld.getYear();
System.out.println(year);
int month = ld.getMonthValue();
System.out.println(month);
int day = ld.getDayOfMonth();
System.out.println(day);
int dayOfYear = ld.getDayOfYear();
System.out.println(dayOfYear);
// 星期几
int dayOfWeek = ld.getDayOfWeek().getValue();
System.out.println(dayOfWeek);
result:
2022-11-14
2022
11
14
318
1
LocalDate
package bistu.time;
import java.time.LocalDate;
public class CalendarTest {
public static void main(String[] args) {
// 0.获取本地日期对象
LocalDate ld = LocalDate.now();
System.out.println(ld);
// 1.获取日期对象中的信息
int year = ld.getYear();
System.out.println(year);
int month = ld.getMonthValue();
System.out.println(month);
int day = ld.getDayOfMonth();
System.out.println(day);
int dayOfYear = ld.getDayOfYear();
System.out.println(dayOfYear);
// 星期几
int dayOfWeek = ld.getDayOfWeek().getValue();
System.out.println(dayOfWeek);
// 2.直接修改某个信息 withYear、withMonth、withDayOfMonth、WithDayOfYear 返回不可变对象
LocalDate ld2 = ld.withYear(2019);
System.out.println(ld2); //2019-11-14
// 3.把某个信息加多少:plusYears、plusMonths、plusDays、plusWeeks
LocalDate ld3 = ld.plusMonths(1);
System.out.println(ld3);
// 4. 把某个信息减多少:minusYears、minusMonths、minusDays、minusWeeks
LocalDate ld4 = ld.minusYears(1);
System.out.println(ld4); // 2021-11-14
// 5.获取指定日期的LocalDate对象:public static LocalDate of(int year,int month, int dayOfMonth)
LocalDate ld5 = LocalDate.of(2019, 9, 3);
System.out.println(ld5); // 2019-09-03
// 6.判断两个日期对象 :equals、isBefore、isAfter
System.out.println(ld5.isAfter(ld4)); //false
}
}
package bistu.time;
import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class CalendarTest {
public static void main(String[] args) {
// 1.ZoneId的常见方法
//public static ZoneId systemDefault:获取系统默认的时区
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId.getId()); // 系统默认的时区 result:Asia/Shanghai
// public static Set getAvailableZoneIds():获取Java支持的全部时区id
System.out.println(ZoneId.getAvailableZoneIds());
// public static ZoneId of(String zonId):把某个时区id封装成ZoneId对象
ZoneId zoneId1 = ZoneId.of("America/New_York");
System.out.println(zoneId1); // America/New_York
// 2、ZonedDateTime:带时区的时间
// public static ZonedDateTime now(ZoneId zone): 获取某个时区的ZonedDateTime对象
ZonedDateTime now = ZonedDateTime.now(zoneId1);
System.out.println(now); // 2022-11-14T10:08:00.363-05:00[America/New_York]
System.out.println(ZonedDateTime.now(Clock.systemUTC())); //世界标准时间
//public static ZonedDateTime now(): 获取系统默认时区的ZonedDateTime对象
ZonedDateTime now2 = ZonedDateTime.now();
System.out.println(now2); // 2022-11-14T23:09:37.932+08:00[Asia/Shanghai]
}
}
Calendar instance = Calendar.getInstance(TimeZone.getTimeZone(zoneId1));
System.out.println(instance);
1.课程