最简单获取当前时间的方法:
方法一:
1
2
3
4
5
6
7
8
9
10
11
|
import java.util.Calendar;
public class DateTime{
public static void main(String[] args){
Calendar c = Calendar.getInstance();
System.out.println( "当前时间为:" );
System.out.println(c.get(Calendar.YEAR)+ "年" +(c.get(Calendar.MONTH)+ 1 )+ "月" +c.get(Calendar.DATE)+ "日" );
System.out.println(c.get(Calendar.HOUR)+ "点" +c.get(Calendar.MINUTE)+ "分" +c.get(Calendar.SECOND)+ "秒" );
System.out.println( "今天是一年中的第:" + c.get(Calendar.DAY_OF_YEAR) + "天" );
System.out.println( "今天是一年中的第:" + c.get(Calendar.WEEK_OF_YEAR) + "个星期" );
}
}
|
方法二:
1
2
3
4
5
6
7
8
9
|
import java.util.Date;
import java.text.SimpleDateFormat;
public class NowString {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); //设置日期格式
System.out.println(df.format( new Date())); // new Date()为获取当前系统时间
}
}
|
方法三:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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" ); //可以方便地修改日期格式
String hehe = dateFormat.format( now );
System.out.println(hehe);
Calendar c = Calendar.getInstance(); //可以对每个时间域单独修改
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = 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 + "/" + date + " " +hour + ":" +minute + ":" + second);
}
}
|
原文链接:https://www.idaobin.com/archives/366.html