日期工具

H 一天中的小时数(0-23)
k 一天中的小时数(1-24)
K am/pm 中的小时数(0-11)
h am/pm 中的小时数(1-12)


一、dateUtils
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import com.paic.icore.acss.util.enumeration.DateStyle;
import com.paic.pafa.app.biz.service.BusinessServiceException;

/**
* 日期工具类
* @author ZHANGYUNPENG048
*
*/
public class DateUtils {

/**
* 将字符串转换为日期
* @param s
* @param style
*
* */
public static Date strToDate(String s, DateStyle style) {
if (s == null || s.length() == 0)
return null;
SimpleDateFormat df = new SimpleDateFormat(style.value());
try {
Date date = df.parse(s);
return date;
} catch (Exception e) {
return null;
}
}

/**
* 把字符串转换成日期
*
* @param date
* @return String
* @throws BusinessServiceException
*/
public static String dateToStr(Date date, DateStyle style) {
if (date == null)
return "";
SimpleDateFormat df = new SimpleDateFormat(style.value());
try {
String str = df.format(date);
return str;
} catch (Exception e) {
return "";
}
}

/**
* 取得当前日期
*
* @return 形如yyyy-mm-dd格式的日期
*/
public static String getCurrentDate(String pattern) {
java.util.Date curDate = new java.util.Date();
SimpleDateFormat df = new SimpleDateFormat(pattern);

return df.format(curDate);
}

public static String toChineseDateStyle(Date date, DateStyle style) {
if (date == null)
return "";
SimpleDateFormat df = new SimpleDateFormat(style.value(), Locale.CHINA);
try {
String str = df.format(date);
return str;
} catch (Exception e) {
return "";
}
}

/**
* 获取总月数
*/
public static long getMonthAmount(Date startDate, Date endDate) {
long monthday;

Calendar starCal = Calendar.getInstance();
starCal.setTime(startDate);

int sYear = starCal.get(Calendar.YEAR);
int sMonth = starCal.get(Calendar.MONTH);
int sDay = starCal.get(Calendar.DATE);

Calendar endCal = Calendar.getInstance();
endCal.setTime(endDate);

int eYear = endCal.get(Calendar.YEAR);
int eMonth = endCal.get(Calendar.MONTH);
int eDay = endCal.get(Calendar.DATE);

monthday = ((eYear - sYear) * 12 + (eMonth - sMonth));

if (sDay < eDay) {
monthday = monthday + 1;
}
return monthday;
}

/**
* 取当前日期date所在月份的最后一天
* @param date
* @return
*/
public static Date getLastDateOfTheMonth(Date date) {
Calendar c1 = Calendar.getInstance();
c1.setTime(date);
c1.set(Calendar.DATE, 1);
c1.roll(Calendar.DATE, -1);
return c1.getTime();
}

public static boolean greatThanCurrentDate(Date date) {
if (new Date().compareTo(date) <= 0) {
return true;
}
return false;
}

}


二、dateStyle
package com.paic.icore.acss.util.enumeration;

public enum DateStyle {

/**
* yyyy-MM-dd HH:mm:ss
*/
FULL_DATE_STYLE("yyyy-MM-dd HH:mm:ss"),

/**
* yyyy-MM-dd
*/
DATE_STYLE("yyyy-MM-dd"),

/**
* yyyy-MM-dd HH:mm
*/
MI_DATE_STYLE("yyyy-MM-dd HH:mm"),

/**
* HH:mm:ss
*/
FULL_TIME_STYLE("HH:mm:ss"),

/**
* yyyyMMdd
*/
STANDAR_DATE_STYLE("yyyyMMdd"),

/**
* yyyy年MM月dd日
*/
DATE_STYLE_ZH_CN("yyyy年MM月dd日"),

/**
* yyyy年MM月dd日 HH时mm分ss秒
*/
DATE_ZH_CN("yyyy年MM月dd日 HH时mm分ss秒");

private DateStyle(String formatString) {
this.formatString = formatString;
}

private String formatString;

public String value() {
return this.formatString;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值