和日期时间处理相关的类

public class BusinessDate {

public BusinessDate() {
}

/**
* 取得当天日期,格式 2009-02-11
* @return
*/
public static String getToday() {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}

/**
* 取得当天日期时间,格式 2009-02-11 23:9:21
* @return
*/
public static String getTodaytime() {
Calendar cl = new GregorianCalendar();
return getToday() + " " + cl.get(Calendar.HOUR_OF_DAY) + ":" + cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND) + " ";
}

/**
* 取得当前时间,格式 23:12:20
* @return
*/
public static String getTime() {
Calendar cl = new GregorianCalendar();
return cl.get(Calendar.HOUR_OF_DAY) + ":" + cl.get(Calendar.MINUTE) + ":" + cl.get(Calendar.SECOND) + " ";
}

/**
* 取得当前小时
* @return
*/
public static int getHour() {
Calendar cl = new GregorianCalendar();
return cl.get(Calendar.HOUR_OF_DAY);
}

/**
* 取得当前日期 格式为20090211
* @return
*/
public static String getNoFormatToday() {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}

/**
* 取得当前时间 格式为231611
* @return
*/
public static String getNoFormatTime() {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("HHmmss");
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}

/**
* 取得当前年份
* @return
*/
public static String getYear() {
return BusinessDate.getNoFormatToday().substring(0, 4);
}

/**
* 取得当前月份
* @return
*/
public static String getMonth() {
return BusinessDate.getNoFormatToday().substring(4, 6);
}

/**
* 取得当前日
* @return
*/
public static String getDay() {
return BusinessDate.getNoFormatToday().substring(6,8 ) ;
}

/**
* 返回昨天的日期 格式2009-02-10
* @return
*/
public static String getYesterday() {
String strYesterday = "";
Calendar cale = null;
cale = new GregorianCalendar();
cale.add(Calendar.DATE, -1);
strYesterday = BusinessDate.getStrByCalendar(cale);
return strYesterday;
}

public static String getStrByCalendar(Calendar cale) {
return (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(cale.getTime());
}

/**
* 日期字符串的格式转换,例如"2009-02-11"转换为2009年2月11日
* @param sDate
* @return
*/
public static String getChnDateString(String sDate) {
if (sDate == null) {
return null;
}
sDate = sDate.trim();
if (sDate.length() == 7) {
sDate += "-01";
}
StringTokenizer st = new StringTokenizer(sDate, "-");
int year = 2100;
int month = 0;
int day = 1;
try {
year = Integer.parseInt(st.nextToken());
month = Integer.parseInt(st.nextToken()) - 1;
day = Integer.parseInt(st.nextToken());
} catch (Exception e) {
e.printStackTrace();
}
Calendar cl = new GregorianCalendar(year, month, day);
return cl.get(Calendar.YEAR) + "年" + (cl.get(Calendar.MONTH) + 1) + "月" + cl.get(Calendar.DATE) + "日";
}

/**
* 取得某年某月的最后一天
* @param year
* @param month
* @return
*/
public static String getMaxDayOfMonth(int year, int month) {
Calendar cal = new GregorianCalendar(year, month - 1, 1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(cal.getTime());
}

/**
* 取得某年某月的第一天
* @param year
* @param month
* @return
*/
public static String getMinDayOfMonth(int year, int month) {
Calendar cal = new GregorianCalendar(year, month - 1, 1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(cal.getTime());
}

/**
* 取得当天的中文日期,像2006年11月28日 星期二
* @return
*/
public static String getChineseToDay() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E", Locale.CHINESE);
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}

/**
* 取得当天的中文日期,像2006年11月28日 星期二 下午05:06
* @return
*/
public static String getChineseToDayTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E a", Locale.CHINESE);
Calendar cl = new GregorianCalendar();
return sdf.format(cl.getTime());
}

/**
* 根据字符串,取得日期类
* @param sDate
* @return
*/
public static Calendar getDate(String sDate) {
if (sDate == null) {
return null;
}
sDate = sDate.trim();
if (sDate.length() == 7) {
sDate += "-01";
}
StringTokenizer st = new StringTokenizer(sDate, "-");
int year = 2100;
int month = 0;
int day = 1;
try {
year = Integer.parseInt(st.nextToken());
month = Integer.parseInt(st.nextToken()) - 1;
day = Integer.parseInt(st.nextToken());
} catch (Exception e) {
e.printStackTrace();
}
return new GregorianCalendar(year, month, day);
}

/**
* 根据日期类取得日期的字符串形式
* @param sDate
* @return
*/
public static String getDateString(Calendar sDate) {
if (sDate == null) {
return "";
}
return (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(sDate.getTime());
}

/**根据日期类取年月的字符串形式
* @param sDate
* @return
*/
public static String getYearMonth(Calendar sDate) {
if (sDate == null) {
return "";
}
return (new java.text.SimpleDateFormat("yyyy-MM")).format(sDate.getTime());
}

/**比较两个日期类型的字符串,格式为(yyyy-mm-dd)
* 如果cale1大于cale2,返回1
* 如果cale1小于cale2,返回-1
* 如果相等,返回0
* 如果格式错误,返回-2
* @param cale1
* @param cale2
* @return
*/
public static int compareCalendar(String cale1, String cale2) {
Calendar calendar1 = getDate(cale1);
Calendar calendar2 = getDate(cale2);
if (calendar1 == null || calendar2 == null) {
return -2;
}
return calendar1.compareTo(calendar2);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值