Java如何获得系统时间
作者:网络转载 发布时间:[ 2014/4/18 10:54:41 ] 推荐标签:Java 系统
//获取当天时间
public String getNowTime(String dateformat){
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);//可以方便地修改日期格式
String hehe = dateFormat.format(now);
return hehe;
}
// 获得当前日期与本周日相差的天数
private int getMondayPlus() {
Calendar cd = Calendar.getInstance();
// 获得是一周的第几天,星期日是第,星期二是第二天......
int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1; //因为按中国礼拜一作为第所以这里减1
if (dayOfWeek == 1) {
return 0;
} else {
return 1 - dayOfWeek;
}
}
//获得本周一的日期
public String getMondayOFWeek(){
weeks = 0;
int mondayPlus = this.getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}
//获得相应周的周六的日期
public String getSaturday() {
int mondayPlus = this.getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks + 6);
Date monday = currentDate.getTime();
DateFormat df = DateFormat.getDateInstance();
String preMonday = df.format(monday);
return preMonday;
}

sales@spasvo.com