首页基础教程通用类通用Date类
Java SimpleDateFormate.format () Date转换为字符串
java的SimpleDateFormate.format (用于将Date转换为字符串格式。 例如,必须转换为2019-05-01 10:00:00/2019年01月01日15时20分10秒等。
定义
字符串缓冲器格式(datedate )。
传入的Date对象将根据指定的格式输出。 输出的格式如下。
g年代标识
y年
m月
第二天
h时为上午或下午(1~12 )。
h时一整天(0~23 ) ) ) ) ) )。
米分钟
秒
s毫秒
e周
d1年的第几天
是f1月的第几天
w1年的第几周
w1月的第几周
上午/下午标记
k时一整天((1~24 ) ) ) ) ) ) ) ) ) ) )。
k时为上午或下午(0~11 )。
z时区
范例
@Test
publicstaticvoidsimpledateformatformatdemo (
日期now=new date (;
simpledateformatmyfmt=newsimpledateformat (yyyy年MM月dd日HH时MM分ss秒);
simpledateformatmyfmt1=newsimpledateformat (YY/mm/ddhh : mm );
simpledateformatmyfmt2=newsimpledateformat (yyyy-mm-ddhh 3360 mm : ss ); 与now.toLocaleString (等价
simpledateformatmyfmt3=newsimpledateformat (yyyy年MM月dd日HH时MM分ss秒e );
simpledateformatmyfmt4=newsimpledateformat (' 1年的第d日1年的第w周1月的第w周是1日的第k时z时区');
system.out.println (myfmt.format ) now );
系统. out.println (myfm t1.format (now ) );
系统. out.println (myfm T2.format (now ) );
系统. out.println (myfm T3.format (now ) );
system.out.println (myfmt4. format (now ) );
}
请注意,SimpleDateFormat不是线程安全的,SimpleDateFormat.format方法在多线程的情况下会发生严重错误。
版权声明:本文为JAVASCHOOL原创文章,未经本站许可不得转载。