java 操作日期的util,日期工具类,Calender

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

/**
* 查询范围时间 JavaBean
*
* @since 2009-1-6 下午08:07:59
*/
public class DateQuery {

/**
* 用于起始时间
*/
private Date fromDate;

/**
* 用于结束时间
*/
private Date toDate;

/**
* 起始时间的字符串形式
*/
private String fromDateStr;

/**
* 结束时间的字符串形式
*/
private String toDateStr;

private SimpleDateFormat sdf = null;

private boolean isMonth = false;

public DateQuery() {
this(false);
}

public DateQuery(boolean isMonth) {
this.isMonth = isMonth;
if(isMonth) {
// 记录年和月(用于月查询)
sdf = new SimpleDateFormat("yyyy-MM");
} else {
// 记录年月日(用于日期查询)
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
}

public Date getFromDate() {
if(this.fromDate == null && StringUtils.isNotEmpty(this.fromDateStr)) {
this.fromDate = parse(this.fromDateStr);
}
return this.fromDate;
}

public void setFromDate(Date fromDate) {
this.fromDate = fromDate;
if(fromDate != null) {
this.fromDateStr = sdf.format(fromDate);
} else {
this.fromDateStr = null;
}
}

public Date getToDate() {
if(this.toDate == null && StringUtils.isNotEmpty(this.toDateStr)) {
this.toDate = parse(this.toDateStr);
}
return toDate;
}

public void setToDate(Date toDate) {
this.toDate = toDate;
if(toDate != null) {
this.toDateStr = format(this.toDate);
} else {
this.toDateStr = null;
}
}

public String getFromDateStr() {
if(StringUtils.isNotEmpty(this.fromDateStr) && this.fromDate != null) {
this.fromDateStr = format(this.fromDate);
}
return this.fromDateStr;
}

public void setFromDateStr(String fromDateStr) {
this.fromDateStr = fromDateStr;
if(fromDateStr != null) {
this.fromDate = parse(fromDateStr);
}
}

public String getToDateStr() {
if(StringUtils.isNotEmpty(this.toDateStr) && this.toDate != null) {
this.toDateStr = format(this.toDate);
}
return toDateStr;
}

public void setToDateStr(String toDateStr) {
this.toDateStr = toDateStr;
if(toDateStr != null) {
this.toDate = parse(toDateStr);
}
}

/**
* 获得结束时间的下一天或者下一月,用于查询条件中
* @return
*
* @since 2009-1-6 下午08:09:54
*/
public Date getNextToDate() {
Date date = getToDate();
if(date == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
int add = isMonth ? Calendar.MONTH : Calendar.DATE;
c.add(add, 1);
return c.getTime();
}

/**
* 将字符串转为 Date
* @param str
* @return
*
* @since 2009-1-6 下午08:10:15
*/
private Date parse(String str) {
Date date = null;
try {
date = sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}

/**
* 格式化字符串
* @param date
* @return
*
* @since 2009-1-6 下午08:10:25
*/
private String format(Date date) {
return sdf.format(date);
}
}


import org.apache.commons.lang.StringUtils;的包在附件中下载
包名是:commons-lang-2.6.jar(或者会有更新的版本)

黑色头发:http://heisetoufa.iteye.com/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.text.SimpleDateFormat; import java.util.*; public class CalendarUtil { public static void main(String args[]) { System.out.println("First day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByWeek(new Date()))); System.out.println("Last day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByWeek(new Date()))); System.out.println("First day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByMonth(new Date()))); System.out.println("Last day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByMonth(new Date()))); } /** * 获得所在星期的第一天 */ public static Date getFirstDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 now.set(Calendar.DATE, first_day_of_week); return now.getTime(); } /** * 获得所在星期的最后一天 */ public static Date getLastDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 int last_day_of_week = first_day_of_week + 6; // 星期日 now.set(Calendar.DATE, last_day_of_week); return now.getTime(); } /** * 获得所在月份的最后一天 * @param 当前月份所在的时间 * @return 月份的最后一天 */ public static Date getLastDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1); now.set(Calendar.DATE, 1); now.set(Calendar.DATE, now.get(Calendar.DATE) - 1); now.set(Calendar.HOUR, 11); now.set(Calendar.MINUTE, 59); now.set(Calendar.SECOND, 59); return now.getTime(); } /** * 获得所在月份的第一天 * @param 当前月份所在的时间 * @return 月份的第一天 */ public static Date getFirstDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.DATE, 0); now.set(Calendar.HOUR, 12); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); return now.getTime(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值