java SimpleDateFormat

转:https://blog.csdn.net/qq_40723205/article/details/79744406

/*SimpleDateFormat日期-时间格式模式参数:

  

字母  日期或时间元素 表示  示例

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 (我在部署的时候在本机oracle是返回星期是星期二,而在Oracle的服务器上是返回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 Main3 {

//DateFormat 介绍

public static void main(String[] args) throws ParseException {

Date date1=new Date();

//-------------------------format()方法--------------

//  将日期类型转化为字符串

//G  Era 标志符   AD

SimpleDateFormat s1=new SimpleDateFormat("Gyyyy年MM月dd日");

System.out.println(s1.format(date1));

//w  年中的周数  

SimpleDateFormat s2=new SimpleDateFormat("一年中的第w周");

System.out.println(s2.format(date1));

   //W 月份中的周数  

SimpleDateFormat s3=new SimpleDateFormat("三月的第W周");

System.out.println(s3.format(date1));

//D  年中的天数  

SimpleDateFormat s4=new SimpleDateFormat("一年的第D天");

System.out.println(s4.format(date1));

       //d  月份中的天数  Number  10

SimpleDateFormat s5=new SimpleDateFormat("三月的第d天");

System.out.println(s5.format(date1));

//E  星期中的天数

SimpleDateFormat s6=new SimpleDateFormat("E");

System.out.println(s6.format(date1));

SimpleDateFormat s7=new SimpleDateFormat(

    "一年中的第 D 天 ,第w个星期 ,一个月中第W个星期 ,k时 z时区");

System.out.println(s7.format(date1));

//----------------------parse()方法----------------

  // 将字符串类型 (java.lang.String)转换为日期类型(java.util.Date)

String str1 = "2019年03月28日 星期三 14:57:45";

    SimpleDateFormat sa = new SimpleDateFormat("yyyy年MM月dd日 E HH:mm:ss");

    //parse()方法

    System.out.println(sa.parse(str1));//输出date格式:Thu Mar 28 14:57:45 GMT+08:00 2019

}

}
---------------------
作者:大虎酱
来源:CSDN
原文:https://blog.csdn.net/qq_40723205/article/details/79744406
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/baaigeini/p/10037823.html