java打印当前日期和时间

2025-04-02 20:22:23
推荐回答(4个)
回答1:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println(df.format(new Date()));// new Date()为获取当前系统时间

回答2:

package time;
import java.util.Date;
import java.util.Calendar;

import java.text.SimpleDateFormat;

public class TestDate{
public static void main(String[] args){
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy/MM/dd");

SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy");

SimpleDateFormat dateFormat3 = new SimpleDateFormat("MM/dd");

String time = dateFormat.format( now );
String date = dateFormat1.format( now );
System.out.println(time);
System.out.println(date);
/*
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date1 = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println(year + "-" + month + "-" + date1 + " " +hour + ":" +minute + ":" + second);
*/
}
}

回答3:

我的编译器上没用任何问题

回答4:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println(df.format(new Date()));// new Date()为获取当前系统时间

望采纳! 谢谢