zoukankan      html  css  js  c++  java
  • 时间对象与字符串之间的相互转化

     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 }
  • 相关阅读:
    SPOJ 10628 求树上的某条路径上第k小的点
    zoj 2112 动态区间求第k大
    SPOJ QTREE 树链剖分
    FZU 2082 过路费
    bzoj 1036 Tree Count
    POJ 3237
    C
    G
    E
    B. Split a Number(字符串加法)
  • 原文地址:https://www.cnblogs.com/zhangqiling/p/11383499.html
Copyright © 2011-2022 走看看