JAVA时间加工类

Posted IT工程师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA时间加工类相关的知识,希望对你有一定的参考价值。

/**

* 当天凌晨

*/

public static Calendar startOfDay(Calendar c) {
if (c == null) {
return c;
}
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c;
}

/**

* 当天晚上
* @param 
* @return
*/

public static Calendar endOfDay(Calendar c) {
if (c == null) {
return c;
}
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 998);
return c;
}

public static Calendar startOfYear(Integer year) {
if (year == null)
return null;
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DAY_OF_MONTH, 1);
return startOfDay(c);
}

public static Calendar endOfYear(Integer year) {
if (year == null)
return null;
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.DECEMBER);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
return endOfDay(c);
}

/**
* 将给定字符串转换为日期,如果没有设置解析器,则用默认格式(yyyy-MM-dd HH:mm:ss)解析
*
* @param s
* 给定字符串
* @param format
* 解析格式
* @return 日期对象
* @throws ParseException
*/
public static Calendar string2Calendar(String s, DateFormat format) throws ParseException {
if (!StringTools.hasText(s))
return null;
Calendar c = Calendar.getInstance();
Date date = (format == null ? DEFAULT_FORMAT : format).parse(s);
c.setTimeInMillis(date.getTime());
return c;
}

/**
* 将给定字符串转换为日期,如果没有设置解析字符串,则用默认格式(yyyy-MM-dd HH:mm:ss)解析
*
* @param s
* 给定字符串
* @param format
* 解析字符串
* @return 日期对象
* @throws ParseException
*/
public static Calendar string2Calendar(String s, String format) throws ParseException {
return string2Calendar(s, new SimpleDateFormat(StringTools.hasText(format) ? format : DEFAULT_PATTERN));
}

/**
* 将给定字符串按照缺省解析格式(yyyy-MM-dd HH:mm:ss)转换为日期
*
* @param s
* @return
* @throws ParseException
*/
public static Calendar string2Calendar(String s) throws ParseException {
return string2Calendar(s, DEFAULT_PATTERN);
}

/**
* 将给定的日期格式化输出,如果没有设置输出格式,则采用模式格式(yyyy-MM-dd HH:mm:ss)输出
*
* @param c
* 日期
* @param format
* 解析格式
* @return
*/
public static String calendar2String(Calendar c, DateFormat format) {
if (c == null)
return null;
return (format == null ? DEFAULT_FORMAT : format).format(c.getTime());
}

/**
* 将给定的日期格式化输出,如果没有设置输出格式,则采用模式格式(yyyy-MM-dd HH:mm:ss)输出
*
* @param c
* 日期
* @param format
* 解析格式
* @return
*/
public static String calendar2String(Calendar c, String format) {
return calendar2String(c, (StringTools.hasText(format) ? new SimpleDateFormat(format) : DEFAULT_FORMAT));
}

/**
* 将给定的日期格式化输出,采用缺省格式(yyyy-MM-dd HH:mm:ss)
*
* @param c
* 日期
* @return
*/
public static String calendar2String(Calendar c) {
return calendar2String(c, DEFAULT_FORMAT);
}

/**
* 得到一个月的第一日和最后一日
*
* @param monthStr
* @return [0] 月的第一天,[1]月的最后一天
* @throws ParseException
*/
public static Calendar[] getMonthStartAndEnd(String monthStr) throws ParseException {
Calendar[] mse = new Calendar[2];
mse[0] = string2Calendar(monthStr, MONTH_PATTERN);
if (null == mse[0]) {
mse[0] = Calendar.getInstance();
mse[0].set(Calendar.DATE, 1);
}
mse[1] = (Calendar) mse[0].clone();
mse[1].set(Calendar.MONTH, mse[1].get(Calendar.MONTH) + 1);
mse[1].set(Calendar.DATE, mse[1].get(Calendar.DATE) - 1);
mse[1].set(Calendar.HOUR_OF_DAY, 23);
mse[1].set(Calendar.MINUTE, 59);
mse[1].set(Calendar.SECOND, 59);
mse[1].set(Calendar.MILLISECOND, 998);
return mse;
}

/**
* 获取前一个月第一天
*
* @param c
* @return
*/
public static Calendar startOfLastMonth(Calendar c) {
c.add(Calendar.MONTH, -1);
c.set(Calendar.DAY_OF_MONTH, 1);
return startOfDay(c);
}

/**
* 获取前一个月最后一天
*
* @param c
* @return
*/
public static Calendar endOfLastMonth(Calendar c) {
c.set(Calendar.DAY_OF_MONTH, 0);
return endOfDay(c);
}

/**
* 上周开始(周日为一周开始)
*
* @param c
* @return
*/
public static Calendar startOfLastWeek(Calendar c) {
c.add(Calendar.WEEK_OF_YEAR, -1);
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return startOfDay(c);
}

/**
* 上周结束(以周六为结束)
*
* @param c
* @return
*/
public static Calendar endOfLastWeek(Calendar c) {
c.add(Calendar.WEEK_OF_YEAR, -1);
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
return endOfDay(c);
}

/**
* 本周开始(周日为一周开始)
*
* @param c
* @return
*/
public static Calendar startOfWeek(Calendar c) {
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return startOfDay(c);
}

/**
* 本周结束
* 周六为一周结束
*
* @param c
* @return
*/
public static Calendar endOfWeek(Calendar c) {
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
return endOfDay(c);
}

/**
* 取得当月天数
* */
public static int getCurrentMonthLastDay() {
Calendar a = Calendar.getInstance();
a.set(Calendar.DATE, 1);// 把日期设置为当月第一天
a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天
int maxDate = a.get(Calendar.DATE);
return maxDate;
}

/**
* 月初第一天
* */

public static Calendar startOfMonth(Calendar cal) {
cal.set(Calendar.DAY_OF_MONTH, 1);
return startOfDay(cal);
}

/**
* 月末最后一天
* @param c
* @return
*/
public static Calendar endOfMonth(Calendar c) {
c.set(Calendar.DATE, c.getActualMaximum(Calendar.DAY_OF_MONTH));
return endOfDay(c);
}

// 获取当前时间所在年的周数
public static int getWeekOfYear(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setMinimalDaysInFirstWeek(7);
c.setTime(date);
return c.get(Calendar.WEEK_OF_YEAR);
}

/**********************************************************************分割线*******************************************************/

通常财务部会已一年中的某一天为开始到第二年的当天作为财年

以9月26日为例

/**
* 获取偏差值
* 获取9月26日到年末的时间毫秒数
* @return
*/
public static long getDeviationTimeInMillis() {
Calendar nowCalendar = Calendar.getInstance();
nowCalendar.set(Calendar.MONTH, 8);
nowCalendar.set(Calendar.DAY_OF_MONTH, 26);
Calendar startTime = startOfDay(nowCalendar);

Calendar instance = Calendar.getInstance();
Calendar year = startOfYear((instance.get(Calendar.YEAR) + 1));

long time = year.getTimeInMillis() - startTime.getTimeInMillis();

return time;
}

/**
* 获取相对时间
* 获取9月26日到年末的时间毫秒数

* 获取当前时间的毫秒数 + 偏差毫秒数
* @return
*/

public static Calendar getDeviationTime(Calendar c) {
long timeInMillis = c.getTimeInMillis();
long deviationMills = timeInMillis + getDeviationTimeInMillis();
Calendar time = Calendar.getInstance();
time.setTimeInMillis(deviationMills);
// System.err.println("yyyyyyy++"+calendar2String(time));
// System.err.println("dayofyear3++"+(time.get(Calendar.DAY_OF_YEAR)));
return time;
}

/**
* 获取相对周数

* @return
*/

public static Integer getWeekOfYear(Calendar nowTime) throws ParseException {
// = Calendar.getInstance();
// nowTime.add(Calendar.MONTH, 8);
// System.err.println("--NOWTIME--"+
// TimeTools.calendar2String(nowTime));
int nowMonth = nowTime.get(Calendar.MONTH) + 1;
int year = nowTime.get(Calendar.YEAR);
int week = 0;
if (nowMonth > 9) {
String standardTimeStr = year + "-09-26 00:00:00";
Calendar standardTime = TimeTools.string2Calendar(standardTimeStr);
if (standardTime == null) {
System.err.println("-----------错误错误错误错误-------------");
return null;
}
int nowWeekOfYear = nowTime.get(Calendar.WEEK_OF_YEAR);
int standardWeekOfYear = standardTime.get(Calendar.WEEK_OF_YEAR);
week = nowWeekOfYear - standardWeekOfYear;
// System.out.println("nowWeekOfYear++"+nowWeekOfYear+"===standardWeekOfYear++"+standardWeekOfYear+"===++"+week);
} else {
// 去年9月26号后的一周为第一周
// 去年9月26之后的周数 + 当前周数。
String standardTimeStr = (year - 1) + "-09-26 00:00:00";
Calendar standardTime = TimeTools.string2Calendar(standardTimeStr);
if (standardTime == null) {
System.err.println("-----------错误错误错误错误-------------");
return null;
}
// 去年9月26号之后的周
int standardWeekOfYear = standardTime.get(Calendar.WEEK_OF_YEAR) + 1;
// 获取去年总周数
// System.err.println((year-1)+"--------------++"+sumWeek(year-1));
int sumWeek = sumWeek(year - 1);
// 计算去年剩余周数
int otherWeek = sumWeek - standardWeekOfYear;
// 获取当前周数
int nowWeek = nowTime.get(Calendar.WEEK_OF_YEAR);
week = nowWeek + otherWeek;
}

return week;
}

 

//给定年月,又去相对的开始年

public static Calendar startOfDeviationYear(int year, int month) {
Calendar nowCalendar = Calendar.getInstance();
if (month >= 8) {
nowCalendar.set(Calendar.YEAR, year);
} else {
nowCalendar.set(Calendar.YEAR, year - 1);
}
nowCalendar.set(Calendar.MONTH, 8);
nowCalendar.set(Calendar.DAY_OF_MONTH, 26);
return startOfDay(nowCalendar);
}

//给定年月,又去相对的结束年

public static Calendar endOfDeviationYear(int year, int month) {

Calendar nowCalendar = Calendar.getInstance();
if (month >= 8)
nowCalendar.set(Calendar.YEAR, year + 1);
else
nowCalendar.set(Calendar.YEAR, year);

nowCalendar.set(Calendar.MONTH, 8);
nowCalendar.set(Calendar.DAY_OF_MONTH, 25);
return endOfDay(nowCalendar);
}

//给定时间 对应的相对月初

public static Calendar startOfDeviationMonth(Calendar c) {
if (c.get(Calendar.DAY_OF_MONTH) < 26)
c.add(Calendar.MONTH, -1);
c.set(Calendar.DAY_OF_MONTH, 26);
return startOfDay(c);
}

//给定时间 相对 月末

public static Calendar endOfDeviationMonth(Calendar c) {
int dayOfMonth = c.get(Calendar.DAY_OF_MONTH);
if (dayOfMonth > 26)
c.add(Calendar.MONTH, 1);
c.set(Calendar.DAY_OF_MONTH, 25);
return endOfDay(c);
}

//获得当前时间 对应 月份中的天数

public static int getDeviationMonthSumDay() {
Calendar instance = Calendar.getInstance();
Calendar c = getDeviationTime(instance);
return getCurrentMonthSumDay(c);
}

//获得给定时间 对应月份中的天数

private static int getCurrentMonthSumDay(Calendar c) {
c.set(Calendar.DATE, 1);// 把日期设置为当月第一天
c.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天
return c.get(Calendar.DATE);
}

/**
* 本周开始
* 周一开始
* @param c
* @return
*/
public static Calendar startOfWeekWithMonday(Calendar c) {
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return startOfDay(c);
}

/**
* 本周结束
* 周日结束
* @param c
* @return
*/
public static Calendar endOfWeekWithSunday(Calendar c) {
c.add(Calendar.WEEK_OF_YEAR, 1);
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return endOfDay(c);
}

/**
* 下周开始
* 周一开始
* @param c
* @return
*/

public static Calendar startOfNextWeekWithMonday(Calendar c) {
// c.add(Calendar.WEEK_OF_YEAR, 1);
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return startOfDay(c);
}

/**
* 上周开始
* 周一开始
* @param c
* @return
*/
public static Calendar startOfLastWeekWithMonday(Calendar c) {
c.add(Calendar.WEEK_OF_YEAR, -1);
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return startOfDay(c);
}

/**
* 上周结束
* 周日结束
* @param c
* @return
*/
public static Calendar endOfLastWeekWithSunday(Calendar c) {
// c.add(Calendar.WEEK_OF_YEAR, -1);
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return endOfDay(c);
}

 














































































































































































































































































































































































以上是关于JAVA时间加工类的主要内容,如果未能解决你的问题,请参考以下文章

java 龙猫在加工

救命!有那位老大知道加工时间怎样设置

P1248 加工生产调度

[HNOI 2001]产品加工

bzoj1222[HNOI2001]产品加工 背包dp

c_cpp Ñ个作业{1,2,...,N}要在由2台机器M1和M2组成的流水线上完成加工。每个作业加工的顺序都是先在M1上加工,然后在M2上加工.M1和M2加工作业我所需的时间分别为AI和璧