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);
        }
    
    }
  • 相关阅读:
    将数组转换为对象
    react常见组件问题Can't perform a React state update on an unmounted component
    React定位到滚动条底部
    React使用定时器interval
    CSS 如何做到隐藏滚动条的同时可以滚动
    css实现字母或数字强制换行
    如何让div的滚动条在上下左右都出现
    js学习
    JavaScript介绍
    前端跨域
  • 原文地址:https://www.cnblogs.com/liubing2018/p/8485721.html
Copyright © 2011-2022 走看看