您的位置:首页 > 编程语言 > Java开发

JAVA Date初探

2014-05-12 00:00 495 查看
摘要: 参考:传智播客毕老师Java基础视频

字符串日期互转,比较

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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息