package com.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import com.util.CommonDateParseUtil;
/**
* @作者 王建明
* @创建日期 Aug 4, 2013
* @创建时间 8:01:27 PM
* @版本号 V 1.0
*/
public class MainTest {
public static void main(String[] args) throws ParseException {
int year = 2013;
int month = 6;
int mondaysNum = getMondayNumByYearMonth(year, month);
for (int i = 0; i < mondaysNum; i++) {
Date date = getFirstDateByYearMonthWeek(year, month, (i + 1));
System.out.println(year
+ "年"
+ month
+ "月第"
+ (i + 1)
+ "周开始日期为:"
+ CommonDateParseUtil.date2string(date,
CommonDateParseUtil.YYYY_MM_DD));
}
}
/**
* @param sc
* @throws ParseException
* @作者 王建明
* @创建日期 Aug 4, 2013
* @创建时间 8:13:34 PM
* @描述 —— 获取某年某月中周一的总数【返回-1表示传入参数有误!!!】
*/
private static int getMondayNumByYearMonth(int year, int month)
throws ParseException {
if (month >= 13 || month <= 0) {
System.out.println("月份不存在!");
} else {
Date date = getLastDayOfYearMonth(year, month);
// 当前月份的总天数
int jiequ_tians = Integer.parseInt(new SimpleDateFormat("dd")
.format(date));
int tongji = 0;
date = getFirstDayOfYearMonth(year, month);
for (int i = 0; i < jiequ_tians; i++) {
Date temp = getDateFromSourceDate(date, i);
if (getWeekNumByDate(temp) == 1) {
do {
tongji++;
i += 7;
} while (i < jiequ_tians);
}
}
return tongji;
}
return -1;
}
/**
* @param year
* @param month
* @param week
* @return
* @作者 王建明
* @创建日期 Aug 4, 2013
* @创建时间 8:47:08 PM
* @描述 —— 获取指定年月周的周一日期【返回null表示出入参数有误!!!】
*/
public static Date getFirstDateByYearMonthWeek(int year, int month, int week) {
if (month >= 13 || month <= 0) {
System.out.println("月份不存在!");
} else {
Date date = getLastDayOfYearMonth(year, month);
// 当前月份的总天数
int jiequ_tians = Integer.parseInt(new SimpleDateFormat("dd")
.format(date));
int tongji = 0;
date = getFirstDayOfYearMonth(year, month);
for (int i = 0; i < jiequ_tians; i++) {
// 从当月1号开始到当月最后一天遍历数据
Date temp = getDateFromSourceDate(date, i);
if (getWeekNumByDate(temp) == 1) {
// 一旦发现有周一直接加上7
do {
tongji++;
if (tongji == week) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, i + 1);
return calendar.getTime();
}
i += 7;
} while (i < jiequ_tians);
}
}
}
return null;
}
/**
* @param date
* @return
* @作者 王建明
* @创建日期 2013-08-02
* @创建时间 16:13:32
* @描述 —— 获取日期所在周的第一天
*/
public static Date getFirstDayOfWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
Calendar calFirstDayInThisWeek = (Calendar) cal.clone();
calFirstDayInThisWeek.add(Calendar.DATE, cal
.getActualMinimum(Calendar.DAY_OF_WEEK)
- dayOfWeek);
Date dateTemp = calFirstDayInThisWeek.getTime();
return getDateFromSourceDate(dateTemp, 1);
}
/**
* @param date
* @return
* @作者 王建明
* @创建日期 2013-08-02
* @创建时间 16:15:15
* @描述 —— 获取日期所在周的最后一天
*/
public static Date getLastDayOfWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
Calendar calLastDayInThisWeek = (Calendar) cal.clone();
calLastDayInThisWeek.add(Calendar.DATE, cal
.getActualMaximum(Calendar.DAY_OF_WEEK)
- dayOfWeek);
Date dateTemp = calLastDayInThisWeek.getTime();
return getDateFromSourceDate(dateTemp, 1);
}
/**
* @param year
* @param month
* @return
* @作者 王建明
* @创建日期 2013-07-03
* @创建时间 15:53:21
* @描述 —— 获取某年某月的第一天
*/
public static Date getFirstDayOfYearMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DATE, 1);
cal.roll(Calendar.DATE, -1);
// 当前月的第一天
cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
/**
* @param year
* @param month
* @return
* @作者 王建明
* @创建日期 2013-07-03
* @创建时间 15:56:02
* @描述 —— 获取某年某月的最后一天
*/
public static Date getLastDayOfYearMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
// 当前月的最后一天
cal.set(Calendar.DATE, 1);
cal.roll(Calendar.DATE, -1);
return cal.getTime();
}
/**
* @return
* @作者 王建明
* @创建日期 2013-05-22
* @创建时间 09:48:04
* @描述 —— 获取当前星期数字【周一~周日分别为1~7表示】
*/
public static int getWeekNumByDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
return weekDay == 1 ? 7 : weekDay - 1;
}
/**
* @param currentDate
* @param num
* @return
* @作者 王建明
* @创建日期 2013-6-8
* @创建时间 下午2:44:24
* @描述 —— 获取与指定日期距离num天的日期
*/
public static Date getDateFromSourceDate(Date currentDate, int num) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(currentDate);
cal.add(GregorianCalendar.DATE, num);
return cal.getTime();
}
}
package com.util;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CommonDateParseUtil {
public static final String ENG_DATE_FROMAT = "EEE, d MMM yyyy HH:mm:ss z";
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
public static final String YYYY_MM_DD = "yyyy-MM-dd";
public static final String YYYY = "yyyy";
public static final String MM = "MM";
public static final String DD = "dd";
/**
* @param date
* @return
* @作者 王建明
* @创建日期 2012-7-13
* @创建时间 下午12:22:40
* @描述 —— 格式化日期对象
*/
public static Date date2date(Date date, String formatStr) {
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
String str = sdf.format(date);
try {
date = sdf.parse(str);
} catch (Exception e) {
return null;
}
return date;
}
/**
* @param date
* @return
* @作者 王建明
* @创建日期 2012-7-13
* @创建时间 下午12:24:19
* @描述 —— 时间对象转换成字符串
*/
public static String date2string(Date date, String formatStr) {
String strDate = "";
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
strDate = sdf.format(date);
return strDate;
}
/**
* @param date
* @return
* @作者 王建明
* @创建日期 2012-7-13
* @创建时间 下午12:24:19
* @描述 —— sql时间对象转换成字符串
*/
public static String timestamp2string(Timestamp timestamp, String formatStr) {
String strDate = "";
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
strDate = sdf.format(timestamp);
return strDate;
}
/**
* @param dateString
* @param formatStr
* @return
* @作者 王建明
* @创建日期 2012-7-13
* @创建时间 下午1:09:24
* @描述 —— 字符串转换成时间对象
*/
public static Date string2date(String dateString, String formatStr) {
Date formateDate = null;
DateFormat format = new SimpleDateFormat(formatStr);
try {
formateDate = format.parse(dateString);
} catch (ParseException e) {
return null;
}
return formateDate;
}
/**
* @param date
* @return
* @作者 王建明
* @创建日期 2012-10-10
* @创建时间 上午09:18:36
* @描述 —— Date类型转换为Timestamp类型
*/
public static Timestamp date2timestamp(Date date) {
if (date == null)
return null;
return new Timestamp(date.getTime());
}
/**
* @return
* @作者 王建明
* @创建日期 2012-9-13
* @创建时间 下午05:02:57
* @描述 —— 获得当前年份
*/
public static String getNowYear() {
SimpleDateFormat sdf = new SimpleDateFormat(YYYY);
return sdf.format(new Date());
}
/**
* @return
* @作者 王建明
* @创建日期 2012-9-13
* @创建时间 下午05:03:15
* @描述 —— 获得当前月份
*/
public static String getNowMonth() {
SimpleDateFormat sdf = new SimpleDateFormat(MM);
return sdf.format(new Date());
}
/**
* @return
* @作者 王建明
* @创建日期 2013-01-24
* @创建时间 08:41:47
* @描述 —— 获得当前日期中的日
*/
public static String getNowDay() {
SimpleDateFormat sdf = new SimpleDateFormat(DD);
return sdf.format(new Date());
}
/**
* @param time
* @return
* @作者 王建明
* @创建日期 2012-6-17
* @创建时间 上午10:19:31
* @描述 —— 指定时间距离当前时间的中文信息
*/
public static String getLnow(long time) {
Calendar cal = Calendar.getInstance();
long timel = cal.getTimeInMillis() - time;
if (timel / 1000 < 60) {
return "1分钟以内";
} else if (timel / 1000 / 60 < 60) {
return timel / 1000 / 60 + "分钟前";
} else if (timel / 1000 / 60 / 60 < 24) {
return timel / 1000 / 60 / 60 + "小时前";
} else {
return timel / 1000 / 60 / 60 / 24 + "天前";
}
}
}