SimpleDateFormat.format的简单使用小结
来源:互联网 发布:充值系统源码 编辑:程序博客网 时间:2024/05/29 18:05
format的用法 是将当前时间格式转换为指定格式
场景一:给定毫秒数或者当前系统时间,返回指定时间格式 输入
Date date=new Date();//获得系统当前的时间// long date=(long)24979599*60000; //任意毫秒数,可以parse转化为日期类型后getTime获取// long date=1498838705129l; System.out.println(date); SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm"); String form=sd.format(date); return form;
注意:在第二行和第三行抓化为long类型时要强制性long转换,否则会提示type int is out of range
场景二:给定任意时间格式,返回毫秒数
parse转化为Date类型后可以直接获取毫秒。输入2017-06-28T09:52 返回毫秒
String s="2017-06-28T09:52";SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");Date date=sdf.parse(s);System.out.println(date);//Sat Jan 28 09:52:00 CST 2017System.out.println(date.getTime());场景三:将给定格式转换为指定格式
输入 06-29-2017 输出 2017/06/29 先parse转化为date类型,再将其format为指定日期类型
String str = "06-29-2017"; SimpleDateFormat sd = new SimpleDateFormat("MM-dd-yyyy"); Date date = (Date) sd.parse(str); System.out.println(date); sd = new SimpleDateFormat("yyyy/MM/dd"); String strDate = sd.format(date); System.out.println(strDate);
阅读全文
0 0
- SimpleDateFormat.format的简单使用小结
- SimpleDateFormat 的简单使用
- SimpleDateFormat的简单使用
- SimpleDateFormat类的使用小结
- SimpleDateFormat 的 format 方法使用详解
- SimpleDateFormat最简单的使用
- SimpleDateFormat和DateFormat的基本使用(format和prase)
- SimpleDateFormat的parse()与format()
- java,SimpleDateFormat的format()方法
- SimpleDateFormat的parse()与format()
- Java—SimpleDateFormat的简单使用
- SimpleDateFormat的简单应用
- SimpleDateFormat类的两个宝物:format(), parse()
- Java中SimpleDateFormat,String.format的用法
- SimpleDateFormat中parse和format的区别
- SimpleDateFormat 的使用
- SimpleDateFormat的使用详解
- SimpleDateFormat的方法使用
- 2016最新CocoaPods安装和错误解决
- des 3des aes ides rsa 对称加密算法
- MPEG4与.mp4
- 守护进程(Daemon)
- vim如何删除文件中所有东西
- SimpleDateFormat.format的简单使用小结
- Fragment回退栈管理
- 关于 bash:$'\r': command not found 的问题
- 个人博客的搭建
- ViewStub
- SSD5 数据结构 Course Design Exercise 3 解析
- 那么来一波模板吧=。=
- git添加到idea中流程
- Spring Boot系列教程七:Spring boot集成MyBatis