66_常用类_DateFormat和SimpleDateFormat_时间和字符串的互相转换
来源:互联网 发布:淘宝模板软件破解版 编辑:程序博客网 时间:2024/06/05 16:19
DateFormat和SimpleDateFormat类
作用:把时间对象转化成指定格式的字符串。反之,把指定格式的字符串转化成时间对象。
- DateFormat是一个抽象类。一般使用SimpleDateFormat类。
代码
public class TestSimpleDateFormat { public static void main(String[] args) throws ParseException { // new出SimpleDateFormat对象 SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); SimpleDateFormat s2 = new SimpleDateFormat("yyyy-MM-dd"); // 将时间对象转换成字符串 String daytime = s1.format(new Date()); System.out.println(s2.format(new Date())); System.out.println(new SimpleDateFormat("hh:mm:ss").format(new Date())); // 将符合指定格式的字符串转成成时间对象.字符串格式需要和指定格式一致。 String time = "2007-10-7"; Date date = s2.parse(time); System.out.println("date1: " + date); time = "2007-10-7 20:15:30"; date = s1.parse(time); System.out.println("date2: " + date); }}
阅读全文
0 0
- 66_常用类_DateFormat和SimpleDateFormat_时间和字符串的互相转换
- java学习之旅66--常用类_DateFormat和SimpleDateFormat_时间和字符串的互相转换
- 常见对象_DateFormat实现日期和字符串的相互转换
- 常用类第三十一课,DateFormat和SimpleDateFormat,时间和字符串的互相转换
- js字符串和时间戳互相转换
- 时间格式和字符串格式互相转换
- oracle 时间和字符串之间的互相转换
- 时间戳和日期的互相转换
- mysql中字符串和时间互相转换的方法(自动转换及DATE_FORMAT函数)
- C# 字符串和byte的互相转换
- json字符串和对象的互相转换
- 关于字符串和小数的互相转换?
- 列表和字符串互相转换的应用
- 字符串和数字之间的互相转换
- 时间戳和字符串互相转换,以及时间戳转换成时间
- 字符串和unicode互相转换
- python 爬虫中时间和时间戳的互相转换
- Excel 时间戳和时间格式的互相转换
- detached HEAD
- 校验输入框中是否有重复值
- delphi日志模块代码
- STM32常见问题整理
- java jdk配置
- 66_常用类_DateFormat和SimpleDateFormat_时间和字符串的互相转换
- iOS在image上画文字-画图片
- 学习笔记——JAVA设计模式<14>中介者模式
- The Search API
- 升级 phpStudy 中 MySQL 版本至 5.7.17
- [bzoj2763]2763: [JLOI2011]飞行路线 分层图最短路
- SVN 错误:Unable to create pristine install stream
- 5-18 银行业务队列简单模拟 (25分)
- Linux之死锁产生条件&解决方法