欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java输出系统当前的日期(年月日时分秒毫秒)

程序员文章站 2023-11-24 23:21:46
复制代码 代码如下: package test.remote.tools.combine; import java.text.simpledateformat; impor...
复制代码 代码如下:

package test.remote.tools.combine;

import java.text.simpledateformat;
import java.util.calendar;
import java.util.date;
import java.util.gregoriancalendar;

public class testoutdate
{
public static void main(string[] args)
{
//method 1
calendar nowtime = new gregoriancalendar();
string strdatetime="["+string.format("%04d", nowtime.get(calendar.year))+"/"+
string.format("%02d", nowtime.get(calendar.month))+"/" +
string.format("%02d", nowtime.get(calendar.date))+" " +
string.format("%02d", nowtime.get(calendar.hour))+":" +
string.format("%02d", nowtime.get(calendar.minute))+":" +
string.format("%02d", nowtime.get(calendar.second))+"." +
string.format("%03d", nowtime.get(calendar.millisecond))+"]";
system.out.println(strdatetime);

//method 2
string msg="";
date date = new date();
simpledateformat sdf = new simpledateformat("yyyy/mm/dd hh:mm:ss.sss");
msg+="["+sdf.format(date)+"]";

system.out.println(msg);
}
}

运行结果:
[2013/08/09 05:54:32.578]
[2013/09/09 17:54:32.625]