首页 > 编程知识 正文

date转换成年月日时分秒,java获取当前日期年月日

时间:2023-05-03 22:44:09 阅读:49091 作者:3021

Java获取当前时刻的年月日、时分秒的方法:

包com.ob;

导入Java.text.parse exception;

导入Java.text.simple date format;

import java.util.Calendar;

import java.util.Date;

公共类日期测试{

publicstaticvoidmain (字符串[ ] args ) throws ParseException { )。

calendar now=calendar.getinstance (;

System.out.println (年: now.get ) calendar.year );

System.out.println (月: () now.get (calendar.month )1) ) );

System.out.println ('日:'now.get ) Calendar.day_of_month );

System.out.println (时: now.get ) calendar.hour_of_day ) );

System.out.println (分: ) now.get ) calendar.minute );

System.out.println (秒: ) now.get ) calendar.second );

System.out.println (当前时间毫秒) now.getTimeInMillis );

system.out.println(now.gettime );

日期d=new date (;

系统. out.println (d;

simpledateformatsdf=newsimpledateformat (yyyy-mm-ddhh : mm : ss );

stringdatenowstr=SDF.format(d;

System.out.println ('格式化日期: ' dateNowStr );

stringstr=' 2012-1-1317336026336033 '; //与上面的sdf中定义的格式相同

datetoday=SDF.parse(str );

System.out.println ) )字符串已转换为日期。 (托日);

}

}

输出结果:

年: 2012

月: 1

日: 13

时: 17

分: 28

秒: 19

当前时间毫秒数: 1326446899902

Fri Jan 13 17:28:19 CST 2012

Fri Jan 13 17:28:19 CST 2012

格式化后的日期: 2012-01-13 17:28:19

字符串转换日期: Fri Jan 13 17:26:33 CST 2012

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