zoukankan      html  css  js  c++  java
  • java的时间

      先看例子:

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    
    public class DateTest
    {
        public static void main(String[] args)
        {
            Date date = new Date();
            System.out.println("Today**********");
            printDate(date);
            
            GregorianCalendar d = new GregorianCalendar();
            d.add(Calendar.DAY_OF_MONTH, -1);
            Date yesterDate = d.getTime();
            System.out.println("Yesterday**********");
            printDate(yesterDate);
        }
        
        private static void printDate(Date date)
        {
            System.out.printf("epoch毫秒数: %s%n", date.getTime());
            System.out.printf("LONG Date: %s%n", DateFormat.getDateInstance(DateFormat.LONG));
            System.out.printf("SHORT Date: %s%n", DateFormat.getDateInstance(DateFormat.SHORT));
            
            System.out.printf("LONG Time: %s%n", DateFormat.getTimeInstance(DateFormat.LONG));
            System.out.printf("SHORT Time: %s%n", DateFormat.getTimeInstance(DateFormat.SHORT));
            
            System.out.printf("LONG DateTime: %s%n", DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG));
            System.out.printf("SHORT DateTime: %s%n", DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT));
            
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.printf("指定格式: %s%n", df.format(date));
        }
    }

      输出:

    Today**********
    epoch毫秒数: 1534952854644
    LONG Date: java.text.SimpleDateFormat@a87fc158
    SHORT Date: java.text.SimpleDateFormat@d5391ab7
    LONG Time: java.text.SimpleDateFormat@787e63cb
    SHORT Time: java.text.SimpleDateFormat@58715d3
    LONG DateTime: java.text.SimpleDateFormat@a0ba3d93
    SHORT DateTime: java.text.SimpleDateFormat@b5341f2a
    指定格式: 2018-08-22 23:47:34
    Yesterday**********
    epoch毫秒数: 1534866454874
    LONG Date: java.text.SimpleDateFormat@a87fc158
    SHORT Date: java.text.SimpleDateFormat@d5391ab7
    LONG Time: java.text.SimpleDateFormat@787e63cb
    SHORT Time: java.text.SimpleDateFormat@58715d3
    LONG DateTime: java.text.SimpleDateFormat@a0ba3d93
    SHORT DateTime: java.text.SimpleDateFormat@b5341f2a
    指定格式: 2018-08-21 23:47:34
  • 相关阅读:
    [置顶] 文件批量重命名(工具)
    zookeeper源码分析之三客户端发送请求流程
    java set转list,数组与list的转换
    分布式电子邮件系统设计--转载
    redis 模糊删除实现
    eclipse 使用jetty调试时,加依赖工程的源码调试方法
    社交产品后端架构设计--转载
    solr服务器的查询过程
    What is corresponding Cron expression to fire in every X seconds, where X > 60? --转载
    zookeeper源码分析之二客户端启动
  • 原文地址:https://www.cnblogs.com/wuxun1997/p/9515227.html
Copyright © 2011-2022 走看看