首页 > 编程知识 正文

java时分秒毫秒格式(java获取毫秒级时间戳)

时间:2023-05-06 11:43:02 阅读:76096 作者:2987

1 .简单日期格式使用详细信息

publicclassimpledateformatextendsdateformat

简单数据格式是针对每个国家敏感地格式化和分析数据的具体类。 可以进行格式(date - text )、语法分析(text - date )和标准化。

使用SimpleDateFormat,可以选择和启动用户为日期和时间格式指定的方法。 但是,您希望使用日期格式的getTimeInstance、getDateInstance或getDateTimeInstance创建日期/时间格式器。 每个类方法返回以默认格式初始化的日期/时间格式器。 如果需要,可以使用applyPattern方法更改格式设置方法。

简单日期格式函数的继承关系:

java.lang.Object

|

----java.text.Format

|

----java.text.DateFormat

|

----java.text.SimpleDateFormat

下面是一个小例子:

导入Java.text.*;

import java.util.Date;

//*

简单日期格式函数语法:

g年代标识

y年

m月

第二天

h时为上午或下午(1~12 )。

h时一整天(0~23 ) ) ) ) ) )。

米分钟

s毫秒

e周

d1年的第几天

是f1月的第几天

w1年的第几周

w1月的第几周

上午/下午标记

k时一整天((1~24 ) ) ) ) ) ) ) ) ) ) )。

k时为上午或下午(0~11 )。

z时区

*/

公共类格式数据时间{

publicstaticvoidmain (字符串[ ] args ) {

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时区';

日期now=new date (;

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 ) );

system.out.println (now.togmtstring ();

system.out.println (now.tolocalestring ();

system.out.println (now.tostring );

}

}

效果:

2004年12月16日17时24分27秒

04/12/16 17:24

2004-12-16 17:24:27

2004年12月16日17时24分27秒星期四

一年的第351天第一年的第51周一月的第三周位于一天的17点CST时区

16 Dec 2004 09:24:27 GMT

2004-12-16 17:24:27

Thu Dec 16 17:24:27 CST 2004

下面是JavaBean:

公共类格式数据时间{

publicstaticstringtolongdatestring {

simpledateformatmyfmt=newsimpledateformat (yyyy年MM月dd日HH时MM分ss秒e );

returnmyfmt.format(dt;

}

publicstaticstringtoshortdatestring {

simpledateformatmyfmt=newsimpledateformat (YY年MM月dd日HH时MM分);

returnmyfmt.format(dt;

}

publicstaticstringtolongtimestring {

simpledateformatmyfmt=newsimpledateformat (hhmmss SSSS );

returnmyfmt.format(dt;

}

publicstaticstringtoshorttimestring {

simpledateformatmyfmt=newsimpledateformat (YY/mm/ddhh : mm );

returnmyfmt.format(dt;

}

publicstaticvoidmain (字符串[ ] args ) {

日期now=new date (;

system.out.println (formatdatetime.to longdate string (now ) );

system.out.println (formatdatetime.toshortdatestring (now ) );

system.out.println (formatdatetime.tolongtimestring (now ) );

system.out.println (formatdatetime.toshorttimestring (now ) );

}

}

调用的main测试结果:

2004年12月16日17时38分26秒星期四

04年12月16日17时38分

17 38 26 0965

04/12/16 17:38

2.Java简单日期格式英语格式

//2012-01-16

system.out.println ((newsimpledateformat ) (yyyy-mm-DD ).format (new date ) ) ) ) ) ) ) ) ); //16-Jan-2012

system.out.println ((newsimpledateformat (' DD-mmm-yyyy ',Locale.ENGLISH ) ) (newdate ) ); //16-Jan-2012 Monday

system.out.println () newsimpledateformat (' DD-mmm-yyyy eeee ',Locale.ENGLISH ).format ) (newdate ) ); //16-Jan-2012 Monday PM

system.out.println (newsimpledateformat ) DD-mmm-yyyyEEEEaa ',Locale.ENGLISH ).format (new date ) )

3 .与毫秒的相互转换

publicstaticvoidmain (字符串argv [ ] ) {

stringstr=' 2015 aug 2200336037336053.305 ';

simpledateformatsdf=newsimpledateformat (yyymmmddhh : mm : ss.SSS (,Locale.ENGLISH ); long millionSeconds=0; try{

millionseconds=SDF.parse(str ).getTime );

}catch(parseexceptione ) {//todo auto-generated catch块

e .打印堆栈跟踪(;

//毫秒

system.out.println (million seconds;

数据=新数据(million seconds;

system.out.println(SDF.format ) date );

}

输出结果:

1440175073305

2015 Aug 22 00:37:53.305

4 .其他参考链接

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