JAVA Date初探

public class MyDateUtil {
 /**
  * 获取日期之间的天数
  * @param d1
  * @param d2
  * @return 天数
  * @throws ParseException
  */
 public static int getDateDif(Date d1,Date d2) throws ParseException {
  long dif = d1.getTime() - d2.getTime();
  return Math.abs((int)(dif/1000/60/60/24));
 }
 
 /**
  * @return 当前日期时间 
  */
 public static String getCurDateTime(){
  Date date = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  return sdf.format(date);
 }
 
 /**
  * 
  * @return 当前日期
  */
 public static String getCurDate(){
  Date date = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  return sdf.format(date);
 }
 
 
 public static void main(String[] args) throws Exception {
  Date date = new Date();
  DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
  String time = df.format(date);
  System.out.println(time);
  
  df = new SimpleDateFormat("yyyy年M月dd日   E  a HH时mm分");//2014年5月12日   星期一  上午 01时25分
  time = df.format(date);
  System.out.println(time);
  
  
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  Date date1 = sdf.parse("2014-04-19");
  Date date2 = sdf.parse("2014-05-19");
  System.out.println(getDateDif(date1,date2));
  System.out.println(getCurDateTime());
  System.out.println(getCurDate());
 }
}

 

你可能感兴趣的:(Date,dateformate)