localdatetime 获取天_LocalDateTime的方法总结

@RunWith(SpringRunner.class)

@SpringBootTest

public class DataTimeUtil {

@Test

public void test(){

LocalDateTime local=LocalDateTime.now();

System.out.println("获取默认当前时间为 "+local);

Clock clock=Clock.systemDefaultZone();

System.out.println("获取默认当前的Clock "+clock.toString());

LocalDateTime localDateTime1=LocalDateTime.now(clock);

System.out.println("获取Clock时区的时间为"+localDateTime1);

LocalDate localDate= LocalDate.now();

LocalTime localTime=LocalTime.now();

LocalDateTime localDateTime2=LocalDateTime.of(localDate,localTime);

System.out.println("localDate是 "+localDate+" localTime是 "+localTime+" 获取的localDateTime2 "+localDateTime2);

System.out.println("分割线-------------------------------------------------------------------分割线");

Integer dayOfYear=local.getDayOfYear();

Integer dayOfMonth=local.getDayOfMonth();

DayOfWeek dayOfWeek=local.getDayOfWeek();

System.out.println("获得是年份中的第几天 "+dayOfYear+" 获取月份中的第几天 "+dayOfMonth+" 获得整个星期的星期几 "+dayOfWeek);

Integer year=local.getYear();

Month month=local.getMonth();

Integer hour=local.getHour();

Integer minute=local.getMinute();

Integer second=local.getSecond();

System.out.println("获得年份 "+year+" 获得月份 "+month+" 获得小时 "+hour+" 获得分钟 "+minute+ " 获得秒钟 "+second);

System.out.println("分割线-------------------------------------------------------------------分割线");

LocalDateTime currentTimeWithDayOfYear=local.withDayOfYear(12);

LocalDateTime currentTimeWithDayOfMonth=local.withDayOfMonth(11);

LocalDateTime currentTimeWithYear=local.withYear(2019);

LocalDateTime currentTimeWithMonth=local.withMonth(1);

LocalDateTime currentTimeWithHour=local.withHour(12);

LocalDateTime currentTimeWithMinute=local.withMinute(22);

LocalDateTime currentTimeWithSecond=local.withSecond(12);

System.out.println("年份中天变更 "+currentTimeWithDayOfYear+" 月份中天变更 "+currentTimeWithDayOfMonth+" 年份变更 "+currentTimeWithYear

+"月份变更 "+currentTimeWithMonth+" 时变更 "+currentTimeWithHour+" 分钟变更 "+currentTimeWithMinute

+"秒变更 "+currentTimeWithSecond);

System.out.println("分割线-------------------------------------------------------------------分割线");

LocalDateTime currentTimeAddYear=local.plusYears(1);

LocalDateTime currentTimeAddMonth=local.plusMonths(1);

LocalDateTime currentTimeAddDay=local.plusDays(1);

LocalDateTime currentTimeAddWeek=local.plusWeeks(1);

LocalDateTime currentTimeAddHour=local.plusHours(1);

LocalDateTime currentTimeAddSecond=local.plusSeconds(1);

LocalDateTime currentTimeAddMinutes=local.plusMinutes(1);

System.out.println("加年份 "+currentTimeAddYear+" 加月份 "+currentTimeAddMonth+" 加天 "+currentTimeAddDay

+" 加周 "+currentTimeAddWeek+" 加小时 "+currentTimeAddHour+" 加分钟 "+currentTimeAddMinutes

+" 加秒 "+currentTimeAddSecond);

LocalDateTime currentTimeMinusYear=local.minusYears(1);

LocalDateTime currentTimeMinusMonth=local.minusMonths(1);

LocalDateTime currentTimeMinusWeek=local.minusWeeks(1);

LocalDateTime currentTimeMinusDay=local.minusDays(1);

LocalDateTime currentTimeMinusHour=local.minusHours(1);

LocalDateTime currentTimeMinusMinute=local.minusMinutes(1);

LocalDateTime currentTimeMinusSecond=local.minusSeconds(1);

System.out.println("减年份 "+currentTimeMinusYear+" 减月份 "+currentTimeMinusMonth+" 减天 "+currentTimeMinusDay

+" 减周 "+currentTimeMinusWeek+" 减小时 "+currentTimeMinusHour+" 减分钟 "+currentTimeMinusMinute

+" 减秒 "+currentTimeMinusSecond);

System.out.println("分割线-------------------------------------------------------------------分割线");

DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

//它会把时间格式化为指定格式的字符串 创建实例才能使用format()方法

String nowTime=local.format(formatter);

System.out.println("格式化后的数据时(得到String类型) "+nowTime);

//parse()方法通过LocalDateTime

String str11="2007-12-03T10:15:30";

//注意当String str="2010-1-1 10:10:10"格式会报错

String str="2010-01-01 10:10:10";

LocalDateTime parseTime=LocalDateTime.parse(str11);

LocalDateTime parseTime1=LocalDateTime.parse(str,formatter);

System.out.println("格式化后的数据时(得到LocalDateTime类型) "+ parseTime+

"格式化后的数据时(得到LocalDateTime类型)有俩个参数 "+parseTime1);

//传入的参数是LocalDateTime类型

LocalDateTime date=parseTime;

boolean flag=local.isBefore(date);

boolean flag1=local.isAfter(date);

System.out.println("传入的参数是 "+date+" 是否在传入的数据之前 "+flag+"是否在传入的数据之后 "+flag1);

//是否相等

boolean flag2=local.equals(date);

System.out.println("localDateTime1和localDateTime2是否相等 "+flag2);

boolean flag3=local.isEqual(date);

System.out.println("localDateTime1和localDateTime2是否相等 "+flag3);

String Str=date.toString();

System.out.println("输出字符串 "+Str);

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值