首页 > 编程知识 正文

java string用法,date转换为string

时间:2023-05-05 23:53:37 阅读:145868 作者:3061

首页基础教程通用类通用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原创文章,未经本站许可不得转载。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。