Java中常用时间的常用命令(记了不亏)

Date 日期 (util包下的)

// 以date类型为例
----
Date date = new Date();
0. 将时间戳转换为date类型
long times = 1412654676572L;
Date date = new Date(times);

1. 获取当前时间 毫秒数
date.getTime();

2. date比较
date.before(new Date());  //表示 当前时间小于 new Date(); 
date.after(new Date());   //表示 当前时间大于 new Date(); 

3.instant转换为Date类型
Instant instant = Instant.now();
Date from = Date.from(instant);

Calendar 日历

Calendar calendar = Calendar.getInstance(); // 获取当前时间
1. 获取毫秒数
long timeInMillis = calendar.getTimeInMillis();
2. 获取月
int res = calendar.get(Calendar.MONTH)+1;
3. 获取年
int res = calendar.get(Calendar.YEAR);
4. 获取日
int res = calendar.get(Calendar.DAY_OF_MONTH);
5. 获取小时
int res = calendar.get(Calendar.HOUR_OF_DAY);
6. 获取分钟
int res = calendar.get(Calendar.MINUTE);
7. 获取秒
int res = calendar.get(Calendar.SECOND);


// 动态设置时间
----

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = dateFormat.parse("2021-09-21");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
int res = calendar.get(Calendar.MONTH) + 1;
System.out.println(res);
----


// 添加追加设置动态时间
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 5); // 第二个参数是按照第一个类型过渡多少(年,月,日,时,分,秒)
int res = calendar.get(Calendar.MONTH) + 1;
System.out.println(res);


//动态替换原有时间
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 5);
System.out.println(Calendar.MONTH);
int res = calendar.get(Calendar.MONTH) + 1;
System.out.println(res);


// 时间比较
calendar.after(calenda2)
calendar.befor(calenda2)


// 转换类型
Date time = calenda2.getTime();
Instant instant = calendar.toInstant();

LocalDate(只能是yyyy-MM-dd)

// localdate常用操作
//获取点当前时间对象
LocalDate now = LocalDate.now();//输出 : 2018-09-08
 
//今年是哪一年
int dayofYear = now.getDayOfYear();
 
//今天是哪一月
int month = now.getMonth().getValue();
 
// 今天是几号
int dayofMonth = now.getDayOfMonth();
 
// 今天是周几
int dayofWeek = now.getDayOfWeek().getValue();
 
//设置指定日期的时间对象:
LocalDate appoint = LocalDate.parse("2018-12-07");
 
//计算两个日期相差多少天:
long differ = appoint.toEpochDay()-now.toEpochDay() // differ : 90
 
//指定天数的加减
LocalDate minus = now.minusDays(10);
System.out.println(minus);            //minus : 2018-08-29
LocalDate plus = now.plusDays(10);
System.out.println(plus);             //plus : 2018-09-18
 
//校验两个日期的前后关系
boolean flag = now.isBefore(appoint);
System.out.println(flag);             //flag : true
 
// 获取取本月第1天:
LocalDate firstDayOfThisMonth = now .with(TemporalAdjusters.firstDayOfMonth())
System.out.println("firstDayOfThisMonth = " + firstDayOfThisMonth);   //2018-09-01
 
// 取本月第2天:
LocalDate secondDayOfThisMonth = now .withDayOfMonth(2);
System.out.println("secondDayOfThisMonth = " + secondDayOfThisMonth); //2018-09-02
 
// 取本月最后一天,再也不用计算是28,29,30还是31:
LocalDate lastDayOfThisMonth = now .with(TemporalAdjusters.lastDayOfMonth());
System.out.println("lastDayOfThisMonth = " + lastDayOfThisMonth);     //2018-09-30
 
// 取下月第一天:
LocalDate firstDayOfNextMonth = lastDayOfThisMonth.plusDays(1);
System.out.println("firstDayOfNextMonth = " + firstDayOfNextMonth);   //2018-10-01
 
// 取下月最后一天:
LocalDate lastDayOfNextOfMonth = firstDayOfNextMonth.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("lastDayOfNextOfMonth = " + lastDayOfNextOfMonth); //2018-10-31
 
// 取2019年1月第一个周一,用Calendar要死掉很多脑细胞:
LocalDate special = LocalDate.parse("2019-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println("special = " + special);                           //2019-01-07
 
 
 

参考API

getYear()    int    获取当前日期的年份
getMonth()    Month    获取当前日期的月份对象
getMonthValue()    int    获取当前日期是第几月
getDayOfWeek()    DayOfWeek    表示该对象表示的日期是星期几
getDayOfMonth()    int    表示该对象表示的日期是这个月第几天
getDayOfYear()    int    表示该对象表示的日期是今年第几天
withYear(int year)    LocalDate    修改当前对象的年份
withMonth(int month)    LocalDate    修改当前对象的月份
withDayOfMonth(int dayOfMonth)    LocalDate    修改当前对象在当月的日期
isLeapYear()    boolean    是否是闰年
lengthOfMonth()    int    这个月有多少天
lengthOfYear()    int    该对象表示的年份有多少天(365或者366plusYears(long yearsToAdd)    LocalDate    当前对象增加指定的年份数
plusMonths(long monthsToAdd)    LocalDate    当前对象增加指定的月份数
plusWeeks(long weeksToAdd)    LocalDate    当前对象增加指定的周数
plusDays(long daysToAdd)    LocalDate    当前对象增加指定的天数
minusYears(long yearsToSubtract)    LocalDate    当前对象减去指定的年数
minusMonths(long monthsToSubtract)    LocalDate    当前对象减去注定的月数
minusWeeks(long weeksToSubtract)    LocalDate    当前对象减去指定的周数
minusDays(long daysToSubtract)    LocalDate    当前对象减去指定的天数
compareTo(ChronoLocalDate other)    int    比较当前对象和other对象在时间上的大小,返回值如果为正,则当前对象时间较晚,
isBefore(ChronoLocalDate other)    boolean    比较当前对象日期是否在other对象日期之前
isAfter(ChronoLocalDate other)    boolean    比较当前对象日期是否在other对象日期之后
isEqual(ChronoLocalDate other)    boolean    比较两个日期对象是否相等

参考连接

LocalTime

// 获取当前系统时间
LocalTime now = LocalTime.now();
// 时间转换
LocalTime sixThirty = LocalTime.parse("06:30");
// 小时分钟秒
LocalTime sixThirty1 = LocalTime.of(8, 30,22);
        System.out.println(sixThirty1);
// 原有的基础上添加时间
LocalTime sevenThirty = LocalTime.parse("09:30").plus(1, ChronoUnit.HOURS);
        System.out.println(sevenThirty);
// 获取小时分秒
int hour = LocalTime.parse("06:30").getHour();
int minute = LocalTime.parse("06:30").getMinute();
int second = LocalTime.parse("06:30").getSecond();

// 时间比较
boolean isbefore = LocalTime.parse("06:30").isBefore(LocalTime.parse("07:30"));
// 一天中的最大,最小和中午时间可以通过LocalTime类中的常量获得。在执行数据
// 库查询以查找给定时间范围内的记录时,这非常有用。
// 例如,下面的代码代表23:59:59.99:
LocalTime maxTime = LocalTime.MAX

LocalDateTime(表示日期和时间的组合)

// 获取当时间
LocalDateTime.now();
// 日期转换
LocalDateTime.of(2015, Month.FEBRUARY, 20, 06, 30);
LocalDateTime.parse("2015-02-20T06:30:00");
// 加减时间
localDateTime.plusDays(1);
localDateTime.minusHours(2);
// 获取时间
localDateTime.getMonth();
// 日期时间格式化
LocalDateTime localDateTime = LocalDateTime.of(2015, Month.JANUARY, 25, 6, 30);
// 以下代码传递ISO日期格式以格式化本地日期。结果将是2021-11-29
String localDateString = localDateTime.format(DateTimeFormatter.ISO_DATE);
// 格式化时间
localDateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));

Instant

// 时间操作
Instant now = Instant.now();
System.out.println(now.getEpochSecond()); // 秒
System.out.println(now.toEpochMilli()); // 毫秒


// 时间比较
Instant start = Instant.now();

long sum = 0L;

for (long i = 0L; i <= 50000000000L; i++) {
	sum += i;
}

System.out.println(sum);

Instant end = Instant.now();

System.out.println("耗费时间为:" + Duration.between(start, end).toMillis());//35-3142-15704

// 获取小时
int hour = instant.atZone(ZoneOffset.UTC).getHour();

// 获取分钟
int minute = instant.atZone(ZoneOffset.UTC).getMinute();

// 获取秒
int second = instant.atZone(ZoneOffset.UTC).getSecond();
// 获取年
int res = instant.atZone(ZoneOffset.UTC).getYear();

----------------------------------------------------
#### 通过 instant.atZone(ZoneOffset.UTC) 方法获取信息

SimpleDateFormat

// 格式
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 格式化时间
String data = format.format(new Date());
// 转换为date类型
Date parse = format.parse(data);

DateTimeFormatter

// 字符串转换为日期标准格式
// String --> LocalDate
LocalDate localDate = LocalDate.parse("2019-12-07");
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
System.out.println(LocalDate.parse("2019-10-09").format(pattern));
// String --> LocalTime
LocalTime localTime = LocalTime.parse("07:43:53");

// String -->LocalDateTime
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"); // 12小时

DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 24小时              LocalDate localDate = LocalDate.parse("2019-12-07 07:43:53",formatter);
LocalDate localDate1 = LocalDate.parse("2019-12-07 07:43:53",formatter);
LocalDateTime localDate2 = LocalDateTime.parse("2019-12-07 07:43:53",formatter1);

System.out.println(localDate1);
System.out.println(localDate2);
System.out.println(localDate);
System.out.println(localTime);


-----------------------------------------------------------------------------


// 日期时间类型转换成字符串
// String --> LocalDate
//localDate --> String 
LocalDate localDate = LocalDate.now();
String format1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);    //yyyyMMdd
String format2 = localDate.format(DateTimeFormatter.ISO_DATE);            //yyyy-MM-dd


//2.LocalTime  --> String
LocalTime localTime = LocalTime.now();
String format3 = localTime.format(DateTimeFormatter.ISO_TIME);            //20:19:22.42
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss");
String format4 = localTime.format(formatter);

//3.LocalDateTime  --> String        
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String format5 = localDateTime.format(formatter2);

System.out.println(format1);
System.out.println(format2);
System.out.println(format3);
System.out.println(format4);
System.out.println(format5);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值