zoukankan      html  css  js  c++  java
  • java Date日期类和SimpleDateFormat日期类格式

    ~Date表示特定的时间,精确到毫秒
    ~构造方法:
    public Date()//构造Date对象并初始化为当前系统的时间
    public Date(long date) //1970-1-1 0:0:0到指定的时间的毫秒数

    ~常用方法:
    public long getTime() //1970-1-1 0:0:0到当前的毫秒数
    public long setTime() //设置日期时间
    public boolean befor(Date when) //测试此日期是否在指定日期之前
    public boolean after(Date when) //测试此日期是否在指定日期之后
    public int compareTo (Date anotherDate) //假设当前Date在Date参数之前,则返回<0;当前Date在Date参数之后,则返回>0
    public String toString() //将日期格式转换为字符串格式输出

    ~DateFormat是日期/时间格式化抽象类,它以语言无关的方式格式化并分析日期或时间
    ~日期/时间格式化子类(如SimpleDateFormat)允许进行格式化(也就是日期->文本)、分析(文本->日期)
    ~构造方法:
    public SimlpeDateFormat()
    public SimpleDateFormat(String pattern)
    ~常用方法:
    public final String format(Date date) //Date转为字符串
    public Date parse(String source) //字符串转为Date

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
    
    public class DateDemo {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Date date = new Date();
            System.out.println(date);
            System.out.println(date.getTime());//返回从1970.1.1到现在的毫秒数
            date.setTime(1519807159999L);//修改时间
            System.out.println(date);
            
            DateFormat df1 = null;//DateFormat为抽象类不可以实例化
            DateFormat df2 = null;//同上
            df1 = DateFormat.getDateInstance();//实现子类对象,get日期
            df2 = DateFormat.getDateTimeInstance();//get日期+时间
            System.out.println("Date:"+df1.format(date));//将字符串转换为日期格式,格式固定
            System.out.println("DateTime:"+df2.format(date));//将字符串转换为日期+时间格式,格式固定
            
            DateFormat df3 = null;
            df3 = DateFormat.getDateInstance(DateFormat.FULL,new Locale("zh","CN"));//格式化为中国日期方式。有SHORT,MEDIUM,LONG,FULL四种格式
            System.out.println("Date:"+df3.format(date));//将字符串转换为日期+时间格式,格式固定
            DateFormat df4 = null;
            df4 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,new Locale("zh","CN"));
            System.out.println("DateTime:"+df4.format(date));//将字符串转换为日期+时间格式,格式固定
            
            //SimpleDateFormat子类可以自定义输出格式,更灵活
            String strDate = "2018-2-29 18:30:00.123";
            Date d= null;
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH-mm-ss.SSS");
            try{//如果有格式书写异常,则把异常抛出
                d = sdf1.parse(strDate);
            }catch(Exception e){
            }
            System.out.println(d);
            String str = sdf2.format(d);//把日期按指定格式输出
            System.out.println(str);
        }
    
    }
  • 相关阅读:
    图像检索(image retrieval)- 11
    图像检索(image retrieval)- 10相关
    Mock.js简易教程,脱离后端独立开发,实现增删改查功能
    Azure Monitor (3) 对虚拟机磁盘设置自定义监控
    Azure Monitor (1) 概述
    Azure SQL Managed Instance (2) 备份SQL MI
    Azure Virtual Network (17) Private Link演示
    Azure Virtual Network (16) Private Link
    Azure Virtual Network (15) Service Endpoint演示
    Azure Virtual Network (14) Service Endpoint服务终结点
  • 原文地址:https://www.cnblogs.com/liubing2018/p/8485721.html
Copyright © 2011-2022 走看看