zoukankan      html  css  js  c++  java
  • java 关于时间处理

     1. 在当前时间上加上时、分、秒

    @Test

    public void testAddDate(){

    Date date = new Date();

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    System.out.println("当前时间 :"+sdf.format(date));

    Calendar c = new GregorianCalendar();

    c.setTime(date);

    c.add(Calendar.HOUR, 1);

    c.add(Calendar.MINUTE,30);

    System.out.println("处理后的时间:"+sdf.format(c.getTime()));
    }

    2.字符串格式时间转换为日期型时间

    @Test
    public void testStringToDate(){
    String Date = "2013-04-20 02:23:33";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    try {

    //将字符串格式时间 转换为时间型
    Date date = sdf.parse(Date);

    //以日期型格式输出
    System.out.println(date);

    //将 date 按照 "yyyy-MM-dd hh:mm:ss"格式 格式化为字符串
    String strDate= sdf.format(date);

    //以字符串形式输出
    System.out.println(strDate);

    } catch (ParseException e) {
    e.printStackTrace();
    }
    }

    3.计算两时间之差

    @Test

    public void test(){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
    Date d1 = sdf.parse("1989-08-28 13:23:23");
    Date d2 = sdf.parse("2013-04-20 13:23:23");
    long d = d2.getTime()-d1.getTime();
    long days = d/ (1000 * 60 * 60 * 24);
    System.out.println(days);
    } catch (ParseException e) {
    e.printStackTrace();
    }
    }

  • 相关阅读:
    时间选择器组件之table避坑指南
    ⼯⼚模式
    外观模式
    中介者模式+装饰器模式
    代理模式
    策略模式
    设计模式--------单例模式
    设计模式--------订阅/发布模式 (观察者)
    对ts的研究
    对react的研究20200724
  • 原文地址:https://www.cnblogs.com/zhanggaosong/p/3032551.html
Copyright © 2011-2022 走看看