java的时间方法

public class SimpleDateFormatDemo {
    public static void main(String[] args) throws ParseException {
        String time = "2022-05-21 12:23:23";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.parse(time));
        method();
    }

    private static void method() {
        Date d = new Date();
        SimpleDateFormat sim =new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        System.out.println(sim.format(d));
    }
}

public class SimpleDateFormatTest {
    public static void main(String[] args) throws ParseException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请录入你的生日(xxxx-xx-xx)");
        String birthday = sc.next();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(birthday);
        long start = date.getTime();
        Date date1 = new Date();
        long end = date1.getTime();
        System.out.println("你活了多少"+(end - start)/(1000L*60*60*24*365));
    }
}

public class LocalDateTimeDemo2 {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        now.getYear();   //获取年
        now.getMonthValue();  //获取月份
        now.getDayOfMonth(); //获取月份的第几天
        now.getDayOfYear();  //获取一年中的几天
        now.getDayOfWeek();  //获取星期
        now.getHour();      //获取时间
        now.getMinute(); //获取分钟
        now.plusYears(1); //整加一年
        now.withMonth(6);//修改月份
    }
}

public class LocalDateTimeDemo4 {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss") ;
        String format = localDateTime.format(dateTimeFormatter);

    }
}
  public static void main(String[] args) {
      String str = "2021/11/11 12:12:12";
      DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
      LocalDateTime.parse(str,dateTimeFormatter);

    }
//时间间隔
public class PeriodAndDuration {
    public static void main(String[] args) {
        LocalDate start = LocalDate.of(2021,11,11);
        LocalDate end = LocalDate.of(2022,12,12);
        Period between = Period.between(start, end);
        between.getYears();
        between.getMonths();
        between.getDays();
        LocalDateTime of1 = LocalDateTime.of(2022, 12, 23, 12, 23, 12);
        LocalDateTime of2 = LocalDateTime.of(2020, 10, 23, 12, 21, 12);
        Duration between1 = Duration.between(of1, of2);
        System.out.println(between1.toDays());
        System.out.println(between1.toHours());
        System.out.println(between1.toMinutes());
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值