import java.text.*;
import java.util.*; 1.-----------------------------------------
得到系统当前时间: java.util.Date dt=new java.util.Date();
System.out.print(dt); //输出结果是:Wed Aug 10 11:29:11 CST 2005 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
System.out.print(sdf.format(dt)); //输出结果是:2005-08-10 2.-----------------------------------------
把字符串转化为java.util.Date
方法一:
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
java.util.Date dt=sdf.parse("2005-2-19");
System.out.print(sdf.format(dt)); //输出结果是:2005-2-19 方法二:
java.util.Date dt=null;
DateFormat df=DateFormat.getDateInstance();
dt=df.parse("2005-12-19");
System.out.println(dt); //输出结果为:Mon Dec 19 00:00:00 CST 2005
System.out.println(df.format(dt)); //输出结果为:2005-2-19 3.-----------------------------------------
把字符串转化为java.sql.Date
字符串必须是"yyyy-mm-dd"格式,否则会抛出IllegalArgumentException异常
java.sql.Date sdt=java.sql.Date.valueOf("2005-9-6");
System.out.println(sdt); //输出结果为:2005-9-6 4.-----------------------------------------
TestApp.java public class TestApp { public static void main(String[] args) {
System.out.println("Hello World!");
Date d=new Date();
//System.out.println(d.toLocaleString());
//Calendar cld=Calendar.getInstance();
System.out.println("Calendar.get(Calendar.DATE)"+(Calendar.getInstance().get(Calendar.DATE)));
Date dt=new Date();//Date(103,-5,-6);
System.out.println("getNowYear(Date dt)"+getYear(dt));
System.out.println("getNowMonth(Date dt)"+getMonth(dt));
System.out.println("getNowDate(Date dt)"+getDate(dt));
}
public static int getNowYear(){
return Calendar.getInstance().get(Calendar.YEAR);
}
public static int getYear(Date dt)throws NullPointerException{
if(dt==null){
throw new NullPointerException("日期参数为null");
}else{
Calendar cld=Calendar.getInstance();
cld.setTime(dt);
return cld.get(Calendar.YEAR);
}
}
public static int getNowMonth(){
return 1+Calendar.getInstance().get(Calendar.MONTH);
}
public static int getMonth(Date dt)throws NullPointerException{
if(dt==null){
throw new NullPointerException("日期参数为null");
}else{
Calendar cld=Calendar.getInstance();
cld.setTime(dt);
return 1+cld.get(Calendar.MONTH);
}
}
public static int getNowDate(){
return 1+Calendar.getInstance().get(Calendar.DATE);
}
public static int getDate(Date dt)throws NullPointerException{
if(dt==null){
throw new NullPointerException("日期参数为null");
}else{
Calendar cld=Calendar.getInstance();
cld.setTime(dt);
return cld.get(Calendar.DATE);
}
}
}
// 格式化日期为字符串 "yyyy-MM-dd hh:mm"
public String formatDateTime(Date basicDate, String strFormat) {
SimpleDateFormat df = new SimpleDateFormat(strFormat);
return df.format(basicDate);
} // 格式化日期为字符串 "yyyy-MM-dd hh:mm"
public String formatDateTime(String basicDate, String strFormat) {
SimpleDateFormat df = new SimpleDateFormat("yy-MM-dd hh:mm");
SimpleDateFormat df1 = new SimpleDateFormat(strFormat);
DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG);
Date tmpDate = null;
try {
tmpDate = df1.parse(basicDate);
}
catch (Exception e) {
// 日期型字符串格式错误
return basicDate;
}
return df.format(tmpDate);
} |