package test;
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class MyDate {
public MyDate() {
}
public String getMyDate() {
Date currTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String thisTime=new String(formatter.format(currTime));
return thisTime;
}
public String getYear() {
Calendar cd = Calendar.getInstance();
String year = cd.get(Calendar.YEAR) + "";
return year;
}
public static void main(String[] args) {
MyDate myDate1 = new MyDate();
System.out.println(myDate1.getMyDate());
System.out.println(myDate1.getYear());
}
}
运行结果如下:
2004-05-28 15:16:47
2004
说明:
1. new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")中的HH大写则表明需要24进制的小时,hh小写则得到12进制的小时.
2. Calendar得到的月比实际的小一月,所以取得后需要加1.
Trackback: http://tb.donews.net/TrackBack.aspx?PostId=23066