您的位置:首页 > 其它

时间对象与字符串之间的相互转化

2020-07-20 04:20 183 查看
1 package cn.zhang.test;
2
3 import java.text.DateFormat;
4 import java.text.ParseException;
5 import java.text.SimpleDateFormat;
6 import java.util.Date;
7
8 /**
9   * 测试时间对象与字符串之间的相互转化
10  * DateFormat抽象类与SimpleDateFormat实现类的使用
11  * @author 张涛
12  *
13  */
14 public class TestDateFormat {
15     public static void main(String[] args) throws ParseException {
16
17         /*把时间对象按照"格式字符串指定的格式"转化成相应的字符串*/
18         DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
19         String str1 = df1.format(new Date());
20         System.out.println(str1);
21
22         /*把字符串按照"格式字符串指定的格式"转化成相对应的时间对象*/
23         DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
24         Date d1 = df2.parse("2019-8-20 15:58:20");
25         System.out.println(d1);
26
27         /*测试其他格式字符串,比如说利用D,获得本时间对象是所处年份的第多少天*/
28         DateFormat df3= new SimpleDateFormat("D");
29         String str2 = df3.format(new Date());
30         System.out.println(str2);
31     }
32 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: