//得到当前系统日期
package org.date;
import java.util.Date;
public class DateDemo01 {
public static void main(String[] args) {
Date date = new Date();
System.out.println("当前日期为:"+date);
}
}
//单独取得系统的当前日期(精确到时分秒)
package org.date;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class DateDemo02 {
public static void main(String[] args) {
Calendar calendar = null;
calendar = new GregorianCalendar();
System.out.println("年:"+calendar.get(Calendar.YEAR));
System.out.println("月:"+(calendar.get(Calendar.MONTH)+1));
System.out.println("日:"+calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("时:"+calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("分:"+calendar.get(Calendar.MINUTE));
System.out.println("秒:"+calendar.get(Calendar.SECOND));
System.out.println("毫秒:"+calendar.get(Calendar.MILLISECOND));
}
}
//DateFormat中的默认操作
package org.date;
import java.text.DateFormat;
import java.util.Date;
public class DateDemo03 {
public static void main(String[] args) {
DateFormat df1 = null;
DateFormat df2 = null;
df1 = DateFormat.getDateInstance();
df2 = DateFormat.getDateTimeInstance();
System.out.println("DATE:"+df1.format(new Date()));
System.out.println("DATETIME:"+df2.format(new Date()));
}
}
//指定日期显示风格
package org.date;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class DateDemo04 {
public static void main(String[] args) {
DateFormat df1 = null;
DateFormat df2 = null;
df1 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD,
new Locale("zh","CN"));
df2 = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,
DateFormat.ERA_FIELD,new Locale("zh","CN"));
System.out.println("DATE:"+df1.format(new Date()));
System.out.println("DATETIME:"+df2.format(new Date()));
}
}
//格式化日期操作
package org.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo05 {
public static void main(String[] args) {
String strDate = "2014-07-14 12:16:20.567";
String pat1 = "yyyy-mm-dd hh:mm:ss.SSS";
String pat2 = "yyyy年mm月dd日 hh时mm分ss秒SSS毫秒";
SimpleDateFormat sdf1 = new SimpleDateFormat(pat1);
SimpleDateFormat sdf2 = new SimpleDateFormat(pat2);
Date d = null;
try {
d = sdf1.parse(strDate);
} catch (ParseException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
System.out.println(sdf2.format(d));
}
}
//实例操作——取得完整日期
1,实现一:基于Calendar类
package org.date;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class DateTime {
private Calendar calendar = null;
public DateTime(){
this.calendar = new GregorianCalendar();
}
public String getDate(){
StringBuffer buf = new StringBuffer();
buf.append(calendar.get(Calendar.YEAR)).append("-");
buf.append(calendar.get(Calendar.MONTH)).append("-");
buf.append(addZero(calendar.get(Calendar.DAY_OF_MONTH)+1,2)).append(" ");
buf.append(addZero(calendar.get(Calendar.HOUR_OF_DAY),2)).append(":");
buf.append(addZero(calendar.get(Calendar.MINUTE),2)).append(":");
buf.append(addZero(calendar.get(Calendar.SECOND),2)).append(".");
buf.append(addZero(calendar.get(Calendar.MILLISECOND),2));
return buf.toString();
}
public String getDateComplete(){
StringBuffer buf = new StringBuffer();
buf.append(calendar.get(Calendar.YEAR)).append("年");
buf.append(calendar.get(Calendar.MONTH)).append("月");
buf.append(addZero(calendar.get(Calendar.DAY_OF_MONTH)+1,2)).append("日");
buf.append(addZero(calendar.get(Calendar.HOUR_OF_DAY),2)).append("时");
buf.append(addZero(calendar.get(Calendar.MINUTE),2)).append("分");
buf.append(addZero(calendar.get(Calendar.SECOND),2)).append("秒");
buf.append(addZero(calendar.get(Calendar.MILLISECOND),2)).append("毫秒");
return buf.toString();
}
public String addZero(int num,int len){
StringBuffer s = new StringBuffer();
s.append(num);
while(s.length()<len){
s.insert(0, "0");
}
return s.toString();
}
public String getTimeStamp(){
StringBuffer buf = new StringBuffer();
buf.append(calendar.get(Calendar.YEAR));
buf.append(calendar.get(Calendar.MONTH));
buf.append(addZero(calendar.get(Calendar.DAY_OF_MONTH)+1,2));
buf.append(addZero(calendar.get(Calendar.HOUR_OF_DAY),2));
buf.append(addZero(calendar.get(Calendar.MINUTE),2));
buf.append(addZero(calendar.get(Calendar.SECOND),2));
buf.append(addZero(calendar.get(Calendar.MILLISECOND),2));
return buf.toString();
}
}
package org.date;
public class DateDemo06 {
public static void main(String[] args) {
DateTime dt = new DateTime();
System.out.println("系统日期:"+dt.getDate());
System.out.println("中文日期:"+dt.getDateComplete());
System.out.println("时间戳:"+dt.getTimeStamp());
}
}
2,实现二:基于SimpleDateFormat类
package org.date;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTime01 {
private SimpleDateFormat sdf = null;
public String getDate(){
this.sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss.SSS");
return sdf.format(new Date());
}
public String getDateComplete(){
this.sdf = new SimpleDateFormat("yyyy年mm月dd日hh时mm分ss秒SSS毫秒");
return sdf.format(new Date());
}
public String getTimeStamp(){
this.sdf = new SimpleDateFormat("yyyymmddhhmmssSSS");
return sdf.format(new Date());
}
}
package org.date;
public class DateDemo07 {
public static void main(String[] args) {
DateTime01 dt = new DateTime01();
System.out.println("系统日期:"+dt.getDate());
System.out.println("中文日期:"+dt.getDateComplete());
System.out.println("系统日期:"+dt.getTimeStamp());
}
}