zoukankan      html  css  js  c++  java
  • 日期转化

    package com.sxc.com.wechat;

    import org.junit.Test;

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    /**
    * 日期格式化类(必须掌握)
    * API:
    * G Era 标志符 Text AD
    * y 年 Year 1996; 96
    * M 年中的月份 Month July; Jul; 07
    * w 年中的周数 Number 27
    * W 月份中的周数 Number 2
    * D 年中的天数 Number 189
    * d 月份中的天数 Number 10
    * F 月份中的星期 Number 2
    * E 星期中的天数 Text Tuesday; Tue
    * a Am/pm 标记 Text PM
    * H 一天中的小时数(0-23) Number 0
    * k 一天中的小时数(1-24) Number 24
    * K am/pm 中的小时数(0-11) Number 0
    * h am/pm 中的小时数(1-12) Number 12
    * m 小时中的分钟数 Number 30
    * s 分钟中的秒数 Number 55
    * S 毫秒数 Number 978
    * z 时区 General time zone Pacific Standard Time; PST; GMT-08:00
    * Z 时区 RFC 822 time zone -0800
    */
    public class TestDate {
    @Test
    public void test1() {//String ==> date对象
    String strDate = "2017-04-19 星期三 下午 20:17:38";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd E a HH:mm:ss");
    try {
    Date date = df.parse(strDate);
    System.out.println(date);
    } catch (ParseException e) {
    e.printStackTrace();
    }
    }

    @Test
    public void test2() { //Date ==> date对象
    Date date = new Date();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd E a HH:mm:ss");
    DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    String strdate = df.format(date);
    String strdate1 = df1.format(date);
    System.out.println(strdate);
    System.out.println(strdate1);

    }
    }
  • 相关阅读:
    Disharmony Trees HDU
    Xenia and Bit Operations CodeForces
    Gym
    背包入门
    搜索入门
    Farm Tour POJ
    Flow Problem HDU
    Hie with the Pie POJ
    Building a Space Station POJ
    kuangbin 最短路集合
  • 原文地址:https://www.cnblogs.com/lovenannan/p/9469081.html
Copyright © 2011-2022 走看看