给定某日,所在周的第一天日期和最后一天日期
给定某日,求助所在周的第一天日期和最后一天日期
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;
public class convertWeekByDate {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
// 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
System.out.println(day);
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
String imptimeBegin = sdf.format(cal.getTime());
System.out.println("所在周星期一的日期:" + imptimeBegin);
cal.add(Calendar.DATE, 6);
String imptimeEnd = sdf.format(cal.getTime());
System.out.println("所在周星期日的日期:" + imptimeEnd);
}
}
cal.setFirstDayOfWeek(Calendar.MONDAY);已经设置了周一才是第一天 今天是周四 为什么我输出System.out.println(day);时候显示的是5呢?这不还是按照周日是第一天了么?
而且后面的cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);和cal.add(Calendar.DATE, 6);
是在干什么呢?看了很久也没想出来=。=求指导
虽然有不明白的地方但是这个程序居然运行是正确的=。=
------解决方案--------------------
楼主拿2012-02-05这个日期试一下,就会发现这个程序还是有小bug的
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
给个对时间进行处理的工具类你看看。看对你有帮助没。。。
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;
public class convertWeekByDate {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
// 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
System.out.println(day);
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
String imptimeBegin = sdf.format(cal.getTime());
System.out.println("所在周星期一的日期:" + imptimeBegin);
cal.add(Calendar.DATE, 6);
String imptimeEnd = sdf.format(cal.getTime());
System.out.println("所在周星期日的日期:" + imptimeEnd);
}
}
cal.setFirstDayOfWeek(Calendar.MONDAY);已经设置了周一才是第一天 今天是周四 为什么我输出System.out.println(day);时候显示的是5呢?这不还是按照周日是第一天了么?
而且后面的cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);和cal.add(Calendar.DATE, 6);
是在干什么呢?看了很久也没想出来=。=求指导
虽然有不明白的地方但是这个程序居然运行是正确的=。=
------解决方案--------------------
楼主拿2012-02-05这个日期试一下,就会发现这个程序还是有小bug的
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
给个对时间进行处理的工具类你看看。看对你有帮助没。。。
- Java code
package tools.util; import java.sql.Timestamp; import java.text.MessageFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * <p> * Title: 日期处理类 * </p> * <p> * Description: 日期处理类 * </p> * * @author zbxu * @version $Revision: 1.3 $ $Date: 2006/10/27 11:46:35 $ */ /** * @author zjli * */ public class DateTools { // the following const is to define date format. public static final int FMT_DATE_YYYYMMDD = 1; public static final int FMT_DATE_YYYYMMDD_HHMMSS = 2; public static final int FMT_DATE_HHMMSS = 3; public static final int FMT_DATE_HHMM = 4; /** * 将特定格式的字符串转换成java.util.Date对象 * <p> * * @param strDate * a date string * @param nFmtDate * specific date string format defined in this class. * @return Date * @throws Exception */ public static Date parseDate(String strDate, int nFmtDate) throws Exception { SimpleDateFormat fmtDate = new SimpleDateFormat(); switch (nFmtDate) { default: case DateTools.FMT_DATE_YYYYMMDD: fmtDate.applyLocalizedPattern("yyyy-MM-dd"); break; case DateTools.FMT_DATE_YYYYMMDD_HHMMSS: fmtDate.applyLocalizedPattern("yyyy-MM-dd HH:mm:ss"); break; case DateTools.FMT_DATE_HHMM: fmtDate.applyLocalizedPattern("HH:mm"); break; case DateTools.FMT_DATE_HHMMSS: fmtDate.applyLocalizedPattern("HH:mm:ss"); break; } return fmtDate.parse(strDate); } /** * <p> * 将两个日期进行比较,获取其相差的年份 * </p> * * @param compDate1 * :指定的日期1 * @param compDate2 * :指定的日期2 * @return 相差的年份 */ public static long getAppoinDate(java.util.Date compDate1, java.util.Date compDate2) { if (compDate1 == null || compDate2 == null) { return -1; } long nYear = (compDate1.getTime() - compDate2.getTime()) / (24 * 3600 * 1000) / 365; return nYear; } /** * * <p> * 比较时间应该在form.tmBegin.value,form.tmEnd.value之前 * </p> * * @param tmBegin * @param tmEnd * @param tmAlert * @return boolean */ public static boolean compareTime(Date tmBegin, Date tmEnd, Date tmAlert) { boolean bReturn = false; long lBegin = tmBegin.getTime(); long lEnd = tmEnd.getTime(); long lAlert = tmAlert.getTime(); if (((lBegin - lAlert) < 0) && ((lAlert - lEnd) < 0)) { bReturn = true; } return bReturn; } /** * * <p> * 比较时间tmBegin和tmEnd大小 * </p> * * @param tmBegin * @param tmEnd * @return boolean */ public static boolean compareTime(Date tmBegin, Date tmEnd) { boolean bReturn = false; long lBegin = tmBegin.getTime(); long lEnd = tmEnd.getTime(); if (lBegin < lEnd) { bReturn = true; } return bReturn; } /** * 日期格式的转换 * <p> * * @param ts * Timestamp格式的时间 * @return 日期字符串 */ public static String getDispalyDate(Timestamp ts) { if (ts == null) { return ""; } return MessageFormat.format("{0,time,yyyy-MM-dd}", new Object[] { ts }); } /** * 显示当前日期 * * @return 日期字符串 */ public static String getDisplayDate() { return MessageFormat.format("{0,time,yyyy-MM-dd}", new Object[] { DateTools.getCurrentTime() }); } /** * 按照指定格式显示当前日期 * * @param formatString * @return */ public static String getDisplayDate(String formatString) { return MessageFormat.format("{0,time," + formatString + "}", new Object[] { DateTools.getCurrentTime() }); } /** * 获取两个Timestamp时间之间的天数,不足一天的四舍五入 * @param time1 * @param time2 * @return time1-time2的天数 */ public static int daysBetween(Timestamp time1,Timestamp time2){ int minus = Math.round((time1.getTime()-time2.getTime()) / (3600 * 24 * 1000)); return minus; } /** * 获取两个Timestamp时间之间的天数,不足一天的按一天算 * @param time1 * @param time2 * @return time1-time2的天数 */ public static int daysBetween2(Timestamp time1,Timestamp time2){ time1=getTimestamp(getDispalyDate(time1), "yyyy-MM-dd"); time2=getTimestamp(getDispalyDate(time2), "yyyy-MM-dd"); int minus = Math.round((time1.getTime()-time2.getTime()) / (3600 * 24 * 1000)); return minus; } /** * 获取当前日期 * */ public static String getCurrentDate(){ Calendar cal=Calendar.getInstance(); SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); return format.format(cal.getTime()); } /** * 获取当前日期 * */ public static String getCurrentDate2Str(String formatStr){ Calendar cal=Calendar.getInstance(); SimpleDateFormat format = new SimpleDateFormat(formatStr); return format.format(cal.getTime()); } /** * 将日期字符串转成timestamp * <p> * * @param strDate * 日期字符串 * @return timestamp类型格式的时间 */ public static Timestamp getTimestampTime(String strDate) { if (strDate.length() == 10) { strDate = strDate + " 00:00:00"; } if (strDate.length() == 16) { strDate = strDate + ":00"; } return getTimestamp(strDate, "yyyy-MM-dd HH:mm:ss"); } /** *将日期字符串转成timestamp *<p> * * @param strDate * 日期字符串 * @return timestamp 类型格式的时间 */ public static Timestamp getTimestampTimeLei(String strDate) { if (strDate.length() == 10) { strDate = strDate + " 24:00:00"; } if (strDate.length() == 16) { strDate = strDate + ":24"; } return getTimestamp(strDate, "yyyy-MM-dd HH:mm:ss"); } /** * 将日期字符串转成timestamp * <p> * * @param strDate * 日期字符串 * @return timestamp类型格式的时间 */ public static Timestamp getTimestamp(String strDate) { return getTimestamp(strDate, "yyyy-MM-dd"); } /** * 将日期字符串转成timestamp * <p> * * @param strDate * 日期字符串 * @return timestamp类型格式的时间 */ public static Timestamp getTimestamp(String strDate, String strFormat) { if (strDate == null || strDate.equals("")) { return null; } SimpleDateFormat sdf = new SimpleDateFormat(strFormat); Date date = null; try { date = sdf.parse(strDate); } catch (ParseException pe) { throw new RuntimeException("错误的日期字符串!"); } return new Timestamp(date.getTime()); } /** * 显示时间 * <p> * * @param ts * 时间 * @return 时间字符串 */ public static String getDisplayTime(Timestamp ts) { if (ts == null) { return ""; } return MessageFormat.format("{0,time,yyyy-MM-dd HH:mm:ss}", new Object[] { ts }); } /** * 显示当前时间 * <p> * * @return 时间字符串 */ public static String getDisplayTime() { return MessageFormat.format("{0,time,yyyy-MM-dd HH:mm:ss}", new Object[] { DateTools.getCurrentTime() }); } /** * 获取当前时间 * <p> * * @return 当前时间 */ public static Timestamp getCurrentTime() { return new Timestamp(System.currentTimeMillis()); } /** * 在字串前面增加字符到指定长度 * <p> * * @param str * 字符串 * @param c * 字符 * @param len * 长度 * @return 定长字符串 */ public static String getFixedString(String str, char c, int len) { String strResult = ""; for (int i = 0; i < len - str.length(); i++) { strResult += c; } return strResult + str; } /** * 得到日期 * @param date * @param yue 要计算date日期的几个月前或后的数字 * 如果要计算前几个月的数字,则该参数为负数 * @return */ public static String getDate(Date date, int yue) { GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); gc.setTime(date); gc.add(Calendar.MONTH, yue); return new SimpleDateFormat("yyyy-MM-dd").format(gc.getTime()); } /** * 取得给定月有多少天 * * @param strDate * 年月String(yyyy-mm) * @return */ public static String getLastDay(String strDate) { if (strDate == null || strDate.equals("")) return ""; String lastDay = ""; try { int year = Integer.parseInt(strDate.substring(0, 4)); int month = Integer.parseInt(strDate.substring(5)) - 1; int day = 1; GregorianCalendar gc = new GregorianCalendar(year, month, day); lastDay = "" + gc.getActualMaximum(GregorianCalendar.DATE); } catch (Exception e) { e.printStackTrace(); } return lastDay; } /** * 根据给定格式取得当前日期 * @param format 给定格式,如:yyyy-MM-dd * @return * @throws ParseException */ public static Date getCurrentDate(String format){ SimpleDateFormat sf=new SimpleDateFormat(format); Calendar cal=Calendar.getInstance(); try { return sf.parse(sf.format(cal.getTime())); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 获取当年的01月01号 * @return */ public static String getFirstDateofYear(){ StringBuffer firstDate=new StringBuffer(); Calendar cal=Calendar.getInstance(); SimpleDateFormat format=new SimpleDateFormat("yyyy"); firstDate.append(format.format(cal.getTime())); firstDate.append("-01-01"); return firstDate.toString(); } /** * 取得距离当前日期一个月后的时间 */ public static String getOneMonthDelay(){ Calendar cal=Calendar.getInstance(); cal.add(2, 1); SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); return format.format(cal.getTime()); } }