栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java的时间方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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());
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/922027.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号