zoukankan      html  css  js  c++  java
  • SimpleDateFormat.format的简单使用小结

    format的用法 是将当前时间格式转换为指定格式

    场景一:给定毫秒数或者当前系统时间,返回指定时间格式 输入

    1.         Date date=new Date();//获得系统当前的时间
    2. //      long date=(long)24979599*60000;    //任意毫秒数,可以parse转化为日期类型后getTime获取
    3. //      long date=1498838705129l;
    4.         System.out.println(date);
    5.         SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm");
    6.         String form=sd.format(date);
    7.         return form;

    注意:在第二行和第三行抓化为long类型时要强制性long转换,否则会提示type int is out of range

    场景二:给定任意时间格式,返回毫秒数

    parse转化为Date类型后可以直接获取毫秒。输入2017-06-28T09:52 返回毫秒

    1. String s="2017-06-28T09:52";
    2. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
    3. Date date=sdf.parse(s);
    4. System.out.println(date);//Sat Jan 28 09:52:00 CST 2017
    5. System.out.println(date.getTime());
    场景三:将给定格式转换为指定格式

    输入 06-29-2017  输出  2017/06/29  先parse转化为date类型,再将其format为指定日期类型

    1. String str = "06-29-2017";
    2. SimpleDateFormat sd = new SimpleDateFormat("MM-dd-yyyy");
    3. Date date = (Date) sd.parse(str);
    4. System.out.println(date);
    5. sd = new SimpleDateFormat("yyyy/MM/dd");
    6. String strDate = sd.format(date);
    7. System.out.println(strDate);



  • 相关阅读:
    常用标点符号的英文名称
    2018年阅读随笔记录(持续更新)
    Lookahead and Lookbehind in Regex
    My Answer in Regex Golf
    Words to Use Instead of "Very"
    区块链
    EntityFramework Core 学习系列(一)Creating Model
    推送本地项目至Github遇到的问题以及解决办法记录
    TF-IDF In Scikit-Learn
    译MassTransit 创建消息消费者
  • 原文地址:https://www.cnblogs.com/jpfss/p/10024620.html
Copyright © 2011-2022 走看看