Date, Calendar ,SimpleDateFormat

public class TestSimpleDateFormat {
    public static void main(String[] args) {

        Date date = new Date();
        long time = date.getTime();

        //时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEE a");

        String format1 = format.format(date);
        System.out.println(format1);

        //格式化字符串日期

        String date1 = "2024-04-07 20:29:11";
        try {
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date parse = format2.parse(date1);
            System.out.println(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}

public class TestDate {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date);

        long time = date.getTime();

        //时间毫秒值
        System.out.println(time);

        time += 2*1000;

        Date date1 = new Date(time);
        System.out.println(date1);

        Date date2 = new Date();
        date2.setTime(time);
        System.out.println(date2);
    }
}

public class TestSimpleDateFormat {
    public static void main(String[] args) {

        Date date = new Date();
        long time = date.getTime();

        //时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEE a");

        String format1 = format.format(date);
        System.out.println(format1);

        //格式化字符串日期

        String date1 = "2024-04-07 20:29:11";
        try {
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date parse = format2.parse(date1);
            System.out.println(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}

public class TestCalendar {
    public static void main(String[] args) {

        //系统此刻时间对应的日历对象
        Calendar now = Calendar.getInstance();

        System.out.println(now);

        //取日历中的某个信息
        System.out.println(now.get(Calendar.YEAR));

        //时间毫秒
        System.out.println(now.getTimeInMillis());

        //修改日历中某个值
        now.set(Calendar.MONTH, 9);

        System.out.println(now);

        //为某个信息添加或减少多少

        now.add(Calendar.MONTH, 2);
        now.add(Calendar.MONTH, -3);

        System.out.println(now);
    }

}

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-11 20:34:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-11 20:34:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-11 20:34:01       82 阅读
  4. Python语言-面向对象

    2024-04-11 20:34:01       91 阅读

热门阅读

  1. Spring的Bean标签配置IOC和依赖注入详解

    2024-04-11 20:34:01       37 阅读
  2. 如何用C++判断一个系统是16位、32位还是64位?

    2024-04-11 20:34:01       38 阅读
  3. 何为C++中的协变

    2024-04-11 20:34:01       40 阅读
  4. MySQL中 not in 和 not exists 区别

    2024-04-11 20:34:01       38 阅读
  5. 分布式任务调度:架构、原理与实践

    2024-04-11 20:34:01       37 阅读
  6. 常见分类算法

    2024-04-11 20:34:01       31 阅读
  7. 【无标题】

    2024-04-11 20:34:01       23 阅读
  8. 中科软面试题

    2024-04-11 20:34:01       29 阅读
  9. 常见的正则表达式

    2024-04-11 20:34:01       38 阅读