hutool 日期时间工具-DateUtil

@Test
	public  void  test1(){
		//当前时间
		Date date = DateUtil.date();
		//当前时间
		Date date2 = DateUtil.date(Calendar.getInstance());
		//当前时间
		Date date3 = DateUtil.date(System.currentTimeMillis());
		//当前时间字符串,格式:yyyy-MM-dd HH:mm:ss
		String now = DateUtil.now();
		//当前日期字符串,格式:yyyy-MM-dd
		String today= DateUtil.today();

		System.out.println(date);
		System.out.println(now);
		System.out.println(date2);
		System.out.println(date3);
		System.out.println(today);
		// 2021-11-18 15:33:33
		// 2021-11-18 15:33:33
		// 2021-11-18 15:33:33
		// 2021-11-18 15:33:33
		// 2021-11-18
  	}

	@Test
	public  void  test2(){
		String dateStr = "2017-03-01";
		Date date = DateUtil.parse(dateStr);
		//结果 2017/03/01
		String format = DateUtil.format(date, "yyyy/MM/dd");
		//常用格式的格式化,结果:2017-03-01
		String formatDate = DateUtil.formatDate(date);
		//结果:2017-03-01 00:00:00
		String formatDateTime = DateUtil.formatDateTime(date);
		//结果:00:00:00
		String formatTime = DateUtil.formatTime(date);

		System.out.println(date);
		System.out.println(format);
		System.out.println(formatDate);
		System.out.println(formatDateTime);
		System.out.println(formatTime);
		// 2017-03-01 00:00:00
		// 2017/03/01
		// 2017-03-01
		// 2017-03-01 00:00:00
		// 00:00:00
  	}

	@Test
	public  void  test3(){
		Date date = DateUtil.date();
		//获得年的部分
		int year = DateUtil.year(date);
		//获得月份,从0开始计数
		int month = DateUtil.month(date);
		//获得月份枚举
		Month month1 = DateUtil.monthEnum(date);
		int day = DateUtil.dayOfMonth(date);
		//.....
		System.out.println(date);
		System.out.println(year);
		System.out.println(month);
		System.out.println(month1);
		System.out.println(day);
		//		2021-11-18 15:41:14
		//		2021
		//		10
		//		NOVEMBER
		//		18
	}

	@Test
	public  void  test4(){
		String dateStr = DateUtil.now();
		Date date = DateUtil.parse(dateStr);
		//一天的开始,结果:2017-03-01 00:00:00
		Date beginOfDay = DateUtil.beginOfDay(date);
		//一天的结束,结果:2017-03-01 23:59:59
		Date endOfDay = DateUtil.endOfDay(date);
		System.out.println(beginOfDay);
		System.out.println(endOfDay);
		// 2021-11-18 00:00:00
		// 2021-11-18 23:59:59
  	}

  	//时间偏移
	@Test
	public  void  test5(){
		String dateStr = "2017-03-01 22:33:23";
		Date date = DateUtil.parse(dateStr);
		//结果:2017-03-03 22:33:23
		Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
		//常用偏移,结果:2017-03-04 22:33:23
		DateTime newDate2 = DateUtil.offsetDay(date, 3);
		//常用偏移,结果:2017-03-01 19:33:23
		DateTime newDate3 = DateUtil.offsetHour(date, -3);

		//昨天
		DateTime yesterday = DateUtil.yesterday();
		//明天
		DateUtil.tomorrow();
		//上周
		DateUtil.lastWeek();
		//下周
		DateUtil.nextWeek();
		//上个月
		DateUtil.lastMonth();
		//下个月
		DateUtil.nextMonth();
		System.out.println(yesterday);
		//2021-11-17 15:58:18
	}

	//计算时间差
	@Test
	public  void  test6(){
		String dateStr1 = "2017-03-01 22:33:23";
		Date date1 = DateUtil.parse(dateStr1);

		String dateStr2 = "2017-04-01 23:33:23";
		Date date2 = DateUtil.parse(dateStr2);

		//相差一个月,天,小时,分秒可以修改后面的day实现
		long betweenDay = DateUtil.between(date1, date2, DateUnit.DAY);
		System.out.println(betweenDay);
	}

	//计算时间差    格式化时间差
	@Test
	public  void  test7(){
		String dateStr1 = "2017-03-01 22:33:23";
		Date date1 = DateUtil.parse(dateStr1);
		String dateStr2 = "2017-04-01 23:30:20";
		Date date2 = DateUtil.parse(dateStr2);
		//相差一个月,天,小时,分秒可以修改后面的day实现
		long betweenDay = DateUtil.between(date1, date2, DateUnit.DAY);
		System.out.println(betweenDay);
		//Level.MINUTE表示精确到分
		String formatBetween = DateUtil.formatBetween(date2, date1,BetweenFormatter.Level.SECOND);
		//输出:31天56分57秒
		Console.log(formatBetween);
	}

	//年龄   是否闰年
	@Test
	public  void  test8(){
		//年龄
		DateUtil.ageOfNow("1990-01-30");
		//是否闰年
		DateUtil.isLeapYear(2017);
	}

	@Test
	public  void  test11(){
		TimeInterval timer = DateUtil.timer();
		DateTime of = DateTime.of(new Date());
		Console.log(of);
		//2021-11-18 16:35:11
		Console.log(of.toString("yyyy-MM-dd"));
		Console.log("花费毫秒数:{}",timer.interval());//花费毫秒数
		Console.log(timer.intervalRestart());//返回花费时间,并重置开始时间
		Console.log(timer.intervalMinute());//花费分钟数
	}
	@Test
	public  void  test12(){
		String dateStr = "2017-03-01";
		Object[] a = { "a", "你", "好", "", 1 };
		//转化时间格式
		Date date = Convert.toDate(dateStr);
		Console.log(date);
		//转化为任意类型
		List<?> objects = Convert.convert(List.class,a);
		//毫秒转化为对应多少分钟
		long c= 4535345;
		//结果为:75
		long minutes = Convert.convertTime(c, TimeUnit.MILLISECONDS, TimeUnit.MINUTES);
		//金钱转化为大写
		double d = 67556.32;
		//结果为:"陆万柒仟伍佰伍拾陆元叁角贰分"
		String digitUppercase = Convert.digitToChinese(d);

		//自定义转化
		int f = 3423;
		ConverterRegistry converterRegistry = ConverterRegistry.getInstance();
		String result = converterRegistry.convert(String.class, f);
		Console.log(result);
		//转化其他类型
		Console.log(Convert.toBigDecimal(f,new BigDecimal(0.00)));
	}

你可能感兴趣的:(java,big,data,数据库,sql)