package com.chis.util;
/**
* <p>Title: 时间格式转化与计算</p>
*/
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedHashMap;
import java.util.List;
import java.text.*;
import org.apache.log4j.Logger;
import com.chis.exception.BaseAppException;
public class DateTimeUtil {
public static String[] Chinese = new String[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
public static String[] Chinese1 = new String[] { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
public static String len = "十";
private static Logger log = Logger.getLogger("DateTimeUtil");
public DateTimeUtil() {
}
/**
* 取当前中文日期
*
* @return String
*/
public static String getChineseDate() {
String currentDate = getCurrentDate();
String[] date = currentDate.split("-");
String year = date[0];
String month = date[1];
String day = date[2];
String chineseYear = "";
String chineseMonth = "";
String chineseDay = "";
for (int i = 0; i < year.length(); i++) {
chineseYear += Chinese[Integer.parseInt(year.substring(i, i + 1))];
}
chineseMonth = numberToChinese(month);
chineseDay = numberToChinese(day);
return chineseYear + "年" + chineseMonth + "月" + chineseDay + "日";
}
public static String numberToChinese(String number) {
if (number.charAt(0) == '0') {
return Chinese[Integer.parseInt(number.substring(1))];
} else if (number.charAt(0) == '1') {
if (number.charAt(1) == '0') {
return len;
}
return len + Chinese[Integer.parseInt(number.substring(1))];
} else if (number.charAt(1) == '0') {
return Chinese[Integer.parseInt(number.substring(0, 1))] + len;
}
return Chinese[Integer.parseInt(number.substring(0, 1))] + len + Chinese[Integer.parseInt(number.substring(1))];
}
/**
* 取得系统当前时间
*
* @return String yyyy-mm-dd
*/
public static String getCurrentDate() {
Calendar rightNow = Calendar.getInstance();
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String time = simpleDateFormat.format(date);
return time;
}
/**
* 取得系统当前时间
*
* @return String yyyy.mm.dd
*/
public static String getCurrentTime() {
Calendar rightNow = Calendar.getInstance();
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd");
String time = simpleDateFormat.format(date);
return time;
}
/**
* 取得系统当前时间
*
* @return String yyyy年mm月dd日
*/
public static String getCurrentDate1() {
Calendar rightNow = Calendar.getInstance();
int year = rightNow.get(rightNow.YEAR);
int month = rightNow.get(rightNow.MONTH) + 1;
int day = rightNow.get(rightNow.DATE);
return year + "年" + month + "月" + day + "日";
}
/**
* 取得系统当前时间
*
* @return String yyyy
*/
public static String getYear() {
Calendar rightNow = Calendar.getInstance();
int year = rightNow.get(rightNow.YEAR);
// int month = rightNow.get(rightNow.MONTH) + 1;
// int day = rightNow.get(rightNow.DATE);
return String.valueOf(year);
}
public static String getMonth(String mark) {
Calendar rightNow = Calendar.getInstance();
int month = rightNow.get(rightNow.MONTH) + 1;
if (mark.equals("MM")) {
String rstr = "0" + String.valueOf(month);
rstr = String.valueOf(rstr.charAt(rstr.length() - 2)) + String.valueOf(rstr.charAt(rstr.length() - 1));
return rstr;
} else
return String.valueOf(month);
}
public static String getDay(String mark) {
Calendar rightNow = Calendar.getInstance();
int day = rightNow.get(rightNow.DATE);
if (mark.equals("dd")) {
String rstr = "0" + String.valueOf(day);
rstr = String.valueOf(rstr.charAt(rstr.length() - 2)) + String.valueOf(rstr.charAt(rstr.length() - 1));
return rstr;
} else
return String.valueOf(day);
}
// 取当前旬
public static String getTenday() {
Calendar rightNow = Calendar.getInstance();
int day = rightNow.get(rightNow.DATE);
String rstr = "";
if (day < 11)
rstr = "1";
else if ((day > 10) && (day < 21))
rstr = "2";
else if (day > 20)
rstr = "3";
return rstr;
}
/**
* 取得系统当前时间
*
* @return String yyyymmdd
*/
public static String getCurrentDate2() {
Calendar rightNow = Calendar.getInstance();
int year = rightNow.get(rightNow.YEAR);
int month = rightNow.get(rightNow.MONTH) + 1;
int day = rightNow.get(rightNow.DATE);
return year + "" + month + "" + day;
}
/**
* 取得系统当前时间
*
* @return String yyyy-mm
*/
public static String getCurrentDate3() {
Calendar rightNow = Calendar.getInstance();
int year = rightNow.get(rightNow.YEAR);
int month = rightNow.get(rightNow.MONTH) + 1;
// int day = rightNow.get(rightNow.DATE);
return year + "-" + month;
}
/**
* 取得系统当前时间
*
* @return String yyyy-mm
*/
public static String getCurrentDateYMD() {
Calendar rightNow = Calendar.getInstance();
int year = rightNow.get(rightNow.YEAR);
int month = rightNow.get(rightNow.MONTH) + 1;
int day = rightNow.get(rightNow.DATE);
String month2 = ("0" + String.valueOf(month));
month2 = month2.substring(month2.length() - 2, 2);
return year + "-" + month2 + "-" + day;
}
/**
* 取得系统当前时间,年份+月份
*
* @return String yyyy-mm
*/
public static String getCurrentDateYM() {
Calendar rightNow = Calendar.getInstance();
String year = rightNow.get(rightNow.YEAR) + "";
int month = rightNow.get(rightNow.MONTH) + 1;
String month2 = ("0" + String.valueOf(month));
month2 = month2.substring(month2.length() - 2, 2);
return year + month2;
}
/**
* 取得系统当前时间
*
* @return String yyyyMMddhhmmss
*/
public static String getCurrentDate4() {
Calendar c = Calendar.getInstance();
c.add(c.SECOND, 0);
// 产生随机的3位字符
String random = new String();
java.util.Random rd = new java.util.Random();
random = String.valueOf(rd.nextInt());
return "" + c.get(c.YEAR) + "" + (c.get(c.MONTH) + 1) + "" + c.get(c.DATE) + "" + c.get(c.HOUR_OF_DAY) + "" + c.get(c.MINUTE) + "" + c.get(c.SECOND);
}
/**
* 取得系统当前时间
*
* @return String yyyy-MM-dd hh:mm:ss
*/
/*
* public String getCurrentDate5() { SimpleDateFormat simpleDateFormat =new
* SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); java.util.Date date = new
* java.util.Date(); String time = simpleDateFormat.format(date); return
* time; }
*/
/**
* 取得系统当前时间
*
* @return String yyyy-MM-dd hh:mm:ss
*/
public static String getCurrentDate5() {
Calendar c = Calendar.getInstance();
c.add(c.SECOND, 0);
return "" + c.get(c.YEAR) + "-" + (c.get(c.MONTH) + 1) + "-" + c.get(c.DATE) + " " + c.get(c.HOUR_OF_DAY) + ":" + c.get(c.MINUTE) + ":"
+ c.get(c.SECOND);
}
/**
* 取得系统当前时间
*
* @return String yyyy-MM-dd hh:mm
*/
public static String getCurrentDate7() {
Calendar c = Calendar.getInstance();
c.add(c.SECOND, 0);
return "" + c.get(c.YEAR) + "." + (c.get(c.MONTH) + 1) + "." + c.get(c.DATE) + " " + c.get(c.HOUR_OF_DAY) + ":" + c.get(c.MINUTE);
}
/**
* 取得系统当前时间 常用于订单号
*
* @return String yyMMdd-hhmmss-ms
*/
public static String getCurrentDate6() {
Calendar c = Calendar.getInstance();
c.add(c.SECOND, 0);
String year = c.get(c.YEAR) + "";
return "" + year.subSequence(2, year.length()) + "" + (c.get(c.MONTH) + 1) + "" + c.get(c.DATE) + "-" + c.get(c.HOUR_OF_DAY) + "" + c.get(c.MINUTE)
+ "" + c.get(c.SECOND) + "-" + c.get(c.MILLISECOND);
}
/**
* 取得系统当前时间和随机3位字符作为主键
*
* @return String yyyyMMddhhmmss + ms + random(3)
*/
public static String getCurrentDateId() {
Calendar c = Calendar.getInstance();
c.add(c.SECOND, 0);
// 产生随机的3位字符
String random = new String();
java.util.Random rd = new java.util.Random();
// 产生0-999的数字
random = String.valueOf(rd.nextInt(1000));
String result = "" + c.get(c.YEAR) + DateTimeUtil.dateFormat(c.get(c.MONTH) + 1) + DateTimeUtil.dateFormat(c.get(c.DATE))
+ DateTimeUtil.dateFormat(c.get(c.HOUR_OF_DAY)) + DateTimeUtil.dateFormat(c.get(c.MINUTE)) + "" + DateTimeUtil.dateFormat(c.get(c.SECOND))
+ c.get(c.MILLISECOND) + random;
return result;
}
public static String dateFormat(int time) {
String rTime = String.valueOf(time);
if (rTime.trim().length() <= 1) {
rTime = "0" + rTime;
}
return rTime;
}
/**
* 取得系统当前时间前n个月的相对应的一天
*
* @param n
* int
* @return String yyyy-mm-dd
*/
public static String getNMonthBeforeCurrentDay(int n) {
Calendar c = Calendar.getInstance();
c.add(c.MONTH, -n);
return "" + c.get(c.YEAR) + "-" + (c.get(c.MONTH) + 1) + "-" + c.get(c.DATE);
}
/**
* 取得系统当前时间前n个月的相对应的一天
*
* @param n
* int
* @return String yyyy-mm-dd
*/
public static String getNMonthBeforeCurrentDay0(int n) {
Calendar c = Calendar.getInstance();
c.add(c.MONTH, -n);
String month = String.valueOf(c.get(c.MONTH) + 1);
if (month.length() == 1) {
month = "0" + month;
}
return "" + c.get(c.YEAR) + "-" + month + "-" + c.get(c.DATE);
}
/**
* 取得系统当前时间后n个月的相对应的一天
*
* @param n
* int
* @return String yyyy-mm-dd
*/
public static String getNMonthAfterCurrentDay(int n) {
Calendar c = Calendar.getInstance();
c.add(c.MONTH, n);
return "" + c.get(c.YEAR) + "-" + (c.get(c.MONTH) + 1) + "-" + c.get(c.DATE);
}
/**
* 取得系统当前时间前n天
*
* @param n
* int
* @return String yyyy-mm-dd
*/
public static String getNDayBeforeCurrentDate(int n) {
Calendar c = Calendar.getInstance();
c.add(c.DAY_OF_MONTH, -n);
String day = String.valueOf(c.get(c.DATE));
String month = String.valueOf(c.get(c.MONTH) + 1);
if (day.length() == 1) {
day = "0" + day;
}
if (month.length() == 1) {
month = "0" + month;
}
return "" + c.get(c.YEAR) + "." + month + "." + day;
}
/**
* 取得系统当前时间后n天
*
* @param n
* int
* @return String yyyy-mm-dd
*/
public static String getNDayAfterCurrentDate(int n) {
Calendar c = Calendar.getInstance();
c.add(c.DAY_OF_MONTH, n);
return "" + c.get(c.YEAR) + "-" + (c.get(c.MONTH) + 1) + "-" + c.get(c.DATE);
}
/**
* 取得系统当前时间后n天
*
* @param n
* int
* @return String yyyy.mm.dd
*/
public static String getNDayAfterCurrentDate2(int n) {
Calendar c = Calendar.getInstance();
c.add(c.DAY_OF_MONTH, n);
// 判断月份和日期是否为1位,如果就为就补"0"
String month = String.valueOf(c.get(c.MONTH) + 1);
if (month.length() == 1) {
month = "0" + month;
}
String date = String.valueOf(c.get(c.DATE));
if (date.length() == 1) {
date = "0" + date;
}
return "" + c.get(c.YEAR) + "." + month + "." + date;
}
// ---------------------------------------------------------------------
// 取过去一个时间对应的系统当年的一天
public static String getCurrentDateAfterPastDate(String sPastDate) {
if (sPastDate != null && !sPastDate.equals("")) {
Date date = switchStringToDate(sPastDate);
Calendar c = Calendar.getInstance();
c.setTime(date);
int iPastYear = c.get(c.YEAR);
Calendar c1 = Calendar.getInstance();
int iCurrentYear = c1.get(c1.YEAR);
c.add(c.YEAR, iCurrentYear - iPastYear);
return "" + c.get(c.YEAR) + "-" + (c.get(c.MONTH) + 1) + "-" + c.get(c.DATE);
} else {
return null;
}
}
/**
* 将一个日期字符串转化成日期
*
* @param sDate
* String
* @return Date yyyy.mm.dd
*/
public static Date switchStringToDate(String sDate) {
Date date = null;
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");
date = df.parse(sDate);
} catch (Exception e) {
log.error(new Date() + "日期转换失败:" + SystemUtil.errorMess(e));
//System.out.println("日期转换失败:" + e.getMessage());
}
return date;
}
/**
* 将一个日期字符串转化成日期
*
* @param sDate
* String
* @return Date yyyy-mm-dd
*/
public static Date switchStringToDate1(String sDate) {
Date date = null;
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
date = df.parse(sDate);
} catch (Exception e) {
log.error(new Date() + "日期转换失败:" + SystemUtil.errorMess(e));
//System.out.println("日期转换失败:" + e.getMessage());
}
return date;
}
/**
* 将一个日期字符串转化成日期,如何为空,则返回null;
*
* @param sDate
* String
* @return Date yyyy-mm-dd
*/
public static Date switchStDY_M_D(String sDate) {
if (null == sDate || sDate.length() <= 0) {
return null;
} else {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(sDate);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
/**
* 将一个日期字符串转化成日期
*
* @param sDate
* String
* @return Date yyyy-MM-dd HH:mm:ss
*/
public static Date switchStringToDate2(String sDate) {
Date date = null;
if (sDate == null || sDate.equals("")) {
return date;
} else {
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
date = df.parse(sDate);
} catch (Exception e) {
log.error(new Date() + "日期转换失败:" + SystemUtil.errorMess(e));
//System.out.println("日期转换失败:" + e.getMessage());
}
return date;
}
}
/**
* 将一个日期字符串转化成日期
*
* @param sDate
* String
* @return Date yyyy-MM-dd HH:mm:ss
*/
public static Date switchStringToDate3(String sDate) {
Date date = null;
if (sDate == null || sDate.equals("")) {
return date;
} else {
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = df.parse(sDate);
} catch (Exception e) {
log.error(new Date() + "日期转换失败:" + SystemUtil.errorMess(e));
//System.out.println("日期转换失败:" + e.getMessage());
}
return date;
}
}
/**
* 将一个日期字符串转化成日期
*
* @return Date yyyy-mm-dd
*/
public static Date getCurrentDateTime() {
Date date = null;
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = df.parse(DateTimeUtil.getCurrentDate5());
} catch (Exception e) {
log.error(new Date() + "日期转换失败:" + SystemUtil.errorMess(e));
//System.out.println("日期转换失败:" + e.getMessage());
}
return date;
}
/**
* 将一个日期字符串转化成日期
*
* @return Date yyyy-mm-dd HH:mm:ss
*/
public static Date getDateTime(String dateTime) {
if (dateTime == null || "".equals(dateTime))
return null;
Date date = null;
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = df.parse(dateTime);
} catch (Exception e) {
log.error(new Date() + "日期转换失败:" + SystemUtil.errorMess(e));
//System.out.println("日期转换失败:" + e.getMessage());
}
return date;
}
/**
* 输入两个字符串型的日期,比较两者的大小
*
* @param fromDate
* String
* @param toDate
* String
* @return boolean before为true
*/
public static boolean compareTwoDateBigOrSmall(String fromDate, String toDate) {
Date dateFrom = DateTimeUtil.switchStringToDate(fromDate);
Date dateTo = DateTimeUtil.switchStringToDate(toDate);
if (dateFrom.before(dateTo)) {
return true;
} else {
return false;
}
}
/**
* 输入两个字符串型的日期,比较两者的大小
*
* @param fromDate
* String
* @param toDate
* String
* @return boolean before为true
*/
public static boolean compareTwoDateBigOrSmall1(String fromDate, String toDate) {
Date dateFrom = DateTimeUtil.switchStringToDate1(fromDate);
Date dateTo = DateTimeUtil.switchStringToDate1(toDate);
if (dateFrom.before(dateTo)) {
return true;
} else {
return false;
}
}
/**
* 将一个日期字符串转化成Calendar
*
* @param sDate
* String
* @return Calendar
*/
public static Calendar switchStringToCalendar(String sDate) {
Date date = switchStringToDate(sDate);
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
/**
* 格式:yyyy-mm-dd
*
* @param sDate
* @return
*/
public static Calendar switchStringToCalendar2(String sDate) {
Date date = switchStringToDate1(sDate);
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
/**
* 将一个日期转化成Calendar
*
* @param date
* Date
* @return Calendar
*/
public static Calendar switchStringToCalendar(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
public static String string2Date(String dateString) throws java.lang.Exception {
/*
* DateFormat dateFormat; dateFormat = new
* SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
* dateFormat.setLenient(false); java.util.Date timeDate =
* dateFormat.parse(dateString);//util类型 //java.sql.Date dateTime = new
* java.sql.Date(timeDate.getTime());//sql类型 java.sql.Timestamp dateTime
* = new java.sql.Timestamp(timeDate.getTime());//Timestamp类型
*/
SimpleDateFormat newk = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
SimpleDateFormat old = new SimpleDateFormat("MMM dd hh:mm:ss yyyy");
String strDate = "Mar 22 00:42:00 2002";
Date d = old.parse(strDate);
return newk.format(d);
}
/**
*
* @param dateString
* yyyy-MM-dd
* @return yyyy年MM月dd日
* @throws java.lang.Exception
*/
public static String string2Date1(String dateString) throws java.lang.Exception {
/*
* DateFormat dateFormat; dateFormat = new
* SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
* dateFormat.setLenient(false); java.util.Date timeDate =
* dateFormat.parse(dateString);//util类型 //java.sql.Date dateTime = new
* java.sql.Date(timeDate.getTime());//sql类型 java.sql.Timestamp dateTime
* = new java.sql.Timestamp(timeDate.getTime());//Timestamp类型
*/
SimpleDateFormat newk = new SimpleDateFormat("yyyy年MM月dd日");
// SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
// String strDate = "Mar 22 00:42:00 2002";
Date d = switchStringToDate1(dateString);
return newk.format(d);
}
public static String getChineseStringDate(Date date) throws java.lang.Exception {
SimpleDateFormat newk = new SimpleDateFormat("yyyy年MM月dd日");
return newk.format(date);
}
public static String getStringDate(Date date) {
if (date == null)
return null;
SimpleDateFormat newk = new SimpleDateFormat("yyyy-MM-dd");
return newk.format(date);
}
public static String getStrDateY_M_D(Date date) {
if (date == null)
return "";
SimpleDateFormat newk = new SimpleDateFormat("yyyy-MM-dd");
return newk.format(date);
}
public static String getStringDateHour(Date date) {
SimpleDateFormat newk = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return newk.format(date);
}
public static String getStringDateOnHour(Date date) throws java.lang.Exception {
SimpleDateFormat newk = new SimpleDateFormat("yyyy-MM-dd HH");
return newk.format(date);
}
/**
* 取得某个时间前n个月的相对应的一天
*
* @param sDate
* String
* @param n
* int
* @return String yyyy-mm-dd
*/
public static String getNMonthBeforeOneDay(String sDate, int n) {
Calendar c = switchStringToCalendar(sDate);
c.add(c.MONTH, -n);
return "" + c.get(c.YEAR) + "-" + (c.get(c.MONTH) + 1) + "-" + c.get(c.DATE);
}
/**
* 格式:yyyy-mm-dd
*
* @param sDate
* @param n
* @return
*/
public static String getNMonthBeforeOneDay2(String sDate, int n) {
Calendar c = switchStringToCalendar2(sDate);
c.add(c.MONTH, -n);
return "" + c.get(c.YEAR) + "-" + (c.get(c.MONTH) + 1) + "-" + c.get(c.DATE);
}
// 取得某个时间后n个月的相对应的一天
public static String getNMonthAfterOneDay(String sDate, int n) {
// Calendar c = switchStringToCalendar(sDate);
Calendar c = switchStringToCalendar2(sDate);
c.add(c.MONTH, n);
return "" + c.get(c.YEAR) + "-" + (c.get(c.MONTH) + 1) + "-" + c.get(c.DATE);
}
// 取得某个时间前n天,格式为yyyy-mm-dd
public static String getNDayBeforeOneDate(String sDate, int n) {
// Calendar c = switchStringToCalendar(sDate);
Calendar c = switchStringToCalendar2(sDate);
c.add(c.DAY_OF_MONTH, -n);
return "" + c.get(c.YEAR) + "-" + (c.get(c.MONTH) + 1) + "-" + c.get(c.DATE);
}
// 取得某个时间后n天,格式为yyyy-mm-dd
public static String getNDayAfterOneDate(String sDate, int n) {
Calendar c = switchStringToCalendar(sDate);
c.add(c.DAY_OF_MONTH, n);
return "" + c.get(c.YEAR) + "-" + (c.get(c.MONTH) + 1) + "-" + c.get(c.DATE);
}
// 取得某个时间后n天,格式为yyyy.mm.dd
public static String getNDayAfterOneDate2(String sDate, int n) {
Calendar c = switchStringToCalendar(sDate);
c.add(c.DAY_OF_MONTH, n);
return "" + c.get(c.YEAR) + "." + (c.get(c.MONTH) + 1) + "." + c.get(c.DATE);
}
// 判断系统当前时间是不是润年
public static boolean isRunNian() {
java.util.Calendar rightNow = java.util.Calendar.getInstance();
int year = rightNow.get(rightNow.YEAR);
if (0 == year % 4 && (year % 100 != 0 || year % 400 == 0)) {
return true;
} else {
return false;
}
}
// /**
// * 取得日期的前1年减一天
// * @param date 日期类型
// * @return
// */
// public static Date getCheckDate(Date date) {
// return switchStringToDate1(getNDayBeforeOneDate(
// getNMonthAfterOneDay(getStringDate(date), 12), 1));
// }
/**
* 根据行业类别设置复核日期
*
* @param date
* @return
*/
public static Date getCheckDate(Date date, int year) {
return switchStringToDate1(getNDayBeforeOneDate(getNMonthAfterOneDay(getStringDate(date), 12 * year), 1));
}
/**
* 取得日期的前yearCount年减一天
*
* @param date
* 日期类型
* @param yearCount
* 年数
* @return
*/
public static Date getValidDate(Date date, int yearCount) {
return switchStringToDate1(getNDayBeforeOneDate(getNMonthAfterOneDay(getStringDate(date), 12 * yearCount), 1));
}
/**
* 取得某个时间的月的最后一天
*
* @param sDate
* String
* @return String yyyy-mm-dd
*/
public static String getLastDayMonth(String sDate) {
Calendar c = switchStringToCalendar(sDate);
return "" + c.get(c.YEAR) + "-" + (c.get(c.MONTH) + 1) + "-" + c.DAY_OF_MONTH;
}
/**
* 返回传入的日期是哪个季度
*
* @param sDate
* 日期字串
* @return 季度 1 2 3 4
*/
public static int getQuarter(String sDate) {
int a = 0;
Calendar c = switchStringToCalendar(sDate);
switch (c.get(c.MONTH)) {
case 1:
case 2:
case 3: {
a = 1;
break;
}
case 4:
case 5:
case 6: {
a = 2;
break;
}
case 7:
case 8:
case 9: {
a = 3;
break;
}
case 10:
case 11:
case 12: {
a = 4;
break;
}
}
return a;
}
/**
* Get the date of monday in this week
*
* @return yyyy.MM.dd
*/
public static String getMondayOfThisWeek() {
String strTemp = "";
Calendar c = Calendar.getInstance();
int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
if (dayofweek == 0)
dayofweek = 7;
c.add(Calendar.DATE, -dayofweek + 1);
strTemp = c.get(1) + ".";
if (c.get(2) + 1 < 10)
strTemp += "0";
strTemp = strTemp + (c.get(2) + 1) + ".";
if (c.get(5) < 10)
strTemp += "0";
strTemp += c.get(5);
return strTemp;
}
/**
* Get the date of Friday in this week
*
* @return yyyy.MM.dd
*/
public static String getFridayOfThisWeek() {
String strTemp = "";
Calendar c = Calendar.getInstance();
int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
if (dayofweek == 0)
dayofweek = 7;
c.add(Calendar.DATE, -dayofweek + 5);
strTemp = c.get(1) + ".";
if (c.get(2) + 1 < 10)
strTemp += "0";
strTemp = strTemp + (c.get(2) + 1) + ".";
if (c.get(5) < 10)
strTemp += "0";
strTemp += c.get(5);
return strTemp;
}
/**
* Get the date of sunday in this week
*
* @return yyyy.MM.dd
*/
public static String getSundayOfThisWeek() {
String strTemp = "";
Calendar c = Calendar.getInstance();
int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
if (dayofweek == 0)
dayofweek = 7;
c.add(Calendar.DATE, -dayofweek + 7);
strTemp = c.get(1) + ".";
if (c.get(2) + 1 < 10)
strTemp += "0";
strTemp = strTemp + (c.get(2) + 1) + ".";
if (c.get(5) < 10)
strTemp += "0";
strTemp += c.get(5);
return strTemp;
}
public static String switchStringToDate4(String sd) {
String[] das = sd.split(" ");
String sDate = das[0];
if (sDate.length() <= 1)
sDate = "0" + sDate;
String sMonth = das[1];
String sYear = das[2];
String sTime = das[3];
if ("Jan".equals(sMonth))
sMonth = "01";
else if ("Feb".equals(sMonth))
sMonth = "02";
else if ("Mar".equals(sMonth))
sMonth = "03";
else if ("Apr".equals(sMonth))
sMonth = "04";
else if ("May".equals(sMonth))
sMonth = "05";
else if ("Jun".equals(sMonth))
sMonth = "06";
else if ("Jul".equals(sMonth))
sMonth = "07";
else if ("Aug".equals(sMonth))
sMonth = "08";
else if ("Sep".equals(sMonth))
sMonth = "09";
else if ("Oct".equals(sMonth))
sMonth = "10";
else if ("Nov".equals(sMonth))
sMonth = "11";
else if ("Dec".equals(sMonth))
sMonth = "12";
return sYear + "-" + sMonth + "-" + sDate + " " + sTime;
}
/**
* 将cst时间转为gmt
*
* @param dtime
* @return
*/
public static String getSD(Date dtime) {
String sd = dtime.toGMTString();
sd = switchStringToDate4(sd);
return sd;
}
/**
* 返回年份
*
* @param date
* 日期
* @return 返回年份
*/
public static int getYear(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.YEAR);
}
/**
* 返回月份
*
* @param date
* 日期
* @return 返回月份
*/
public static int getMonth(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.MONTH) + 1;
}
/**
* 返回日期所在一年中的周数
*
* @param date
* 日期
* @return 返回月份
*/
public static int getWeek(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.WEEK_OF_YEAR);
}
/**
* 取得某天是一年中的多少周(取得某一年共有多少周时使用)
*
* @param date
* @return
*/
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);
}
/**
* 取得某一年共有多少周
*
* @param year
* @return
*/
public static int getMaxWeekNumOfYear(int year) {
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
return getWeekOfYear(c.getTime());
}
/**
* 取得坐在周,若周和年交叉,返回该年最大周数加1
*
* @param year
* @param endWeek
* @param endMonth
* @return
*/
public static int getMaxDayWeek(int year, int endWeek, int endMonth) {
if ((endWeek == 1 && endMonth == 12)) {
int weekYear = getMaxWeekNumOfYear(year);
endWeek = weekYear + 1;
}
return endWeek;
}
/**
* 得到周的第一天
*
* @param date
* @return
* @throws Exception
*/
public static Date getWeekFirstDay(int year, int week) throws Exception {
Calendar calFirstDayOfTheYear = new GregorianCalendar(year, Calendar.JANUARY, 1);
calFirstDayOfTheYear.add(Calendar.DATE, 7 * (week - 1));
int dayOfWeek = calFirstDayOfTheYear.get(Calendar.DAY_OF_WEEK);
Calendar calFirstDayInWeek = (Calendar) calFirstDayOfTheYear.clone();
calFirstDayInWeek.add(Calendar.DATE, calFirstDayOfTheYear.getActualMinimum(Calendar.DAY_OF_WEEK) - dayOfWeek);
return calFirstDayInWeek.getTime();
}
/**
* 得到周的最后一天
*
* @param date
* @return
* @throws Exception
*/
public static Date getWeekLastDay(int year, int week) throws Exception {
Calendar calFirstDayOfTheYear = new GregorianCalendar(year, Calendar.JANUARY, 1);
calFirstDayOfTheYear.add(Calendar.DATE, 7 * (week - 1));
int dayOfWeek = calFirstDayOfTheYear.get(Calendar.DAY_OF_WEEK);
Calendar calLastDayInWeek = (Calendar) calFirstDayOfTheYear.clone();
calLastDayInWeek.add(Calendar.DATE, calFirstDayOfTheYear.getActualMaximum(Calendar.DAY_OF_WEEK) - dayOfWeek);
return calLastDayInWeek.getTime();
}
/**
* 获得某一日期的前一天
*
* @param date
* @return Date
*/
public static Date getPreviousDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int day = calendar.get(Calendar.DATE);
calendar.set(Calendar.DATE, day - 1);
return calendar.getTime();
}
/**
* 得到月的第一天
*
* @return
* @throws Exception
*/
public static String getMonthFirstDay(Date date) throws Exception {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return getStringDate(calendar.getTime());
}
/**
* 得到月的最后一天
*
* @return
* @throws Exception
*/
public static String getMonthLastDay(Date date) throws Exception {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return getStringDate(calendar.getTime());
}
/**
* 获得某年某月最后一天的日期
*
* @param year
* @param month
* @return Date
*/
public static Date getLastDayOfMonth(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DATE, 1);
return getPreviousDate(calendar.getTime());
}
/**
* 获得某年某月第一天的日期
*
* @param year
* @param month
* @return Date
*/
public static Date getFirstDayOfMonth(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DATE, 1);
return calendar.getTime();
}
/**
* 获得某年某季度的最后一天的日期
*
* @param year
* @param quarter
* @return Date
*/
public static Date getLastDayOfQuarter(int year, int quarter) {
int month = 0;
if (quarter > 4) {
return null;
} else {
month = quarter * 3;
}
return getLastDayOfMonth(year, month);
}
/**
* 获得某年某季度的第一天的日期
*
* @param year
* @param quarter
* @return Date
*/
public static Date getFirstDayOfQuarter(int year, int quarter) {
int month = 0;
if (quarter > 4) {
return null;
} else {
month = (quarter - 1) * 3 + 1;
}
return getFirstDayOfMonth(year, month);
}
/**
* 获得某年某半年的第一天的日期
*
* @param year
* @param halfYear
* @return Date
*/
public static Date getFirstDayOfHalfYear(int year, int halfYear) {
int month = 0;
if (halfYear > 2) {
return null;
} else {
month = (halfYear - 1) * 6 + 1;
}
return getFirstDayOfMonth(year, month);
}
/**
* 获得某年某半年的最后一天的日期
*
* @param year
* @param quarter
* @return Date
*/
public static Date getLastDayOfHalfYear(int year, int halfYear) {
int month = 0;
if (halfYear > 2) {
return null;
} else {
month = halfYear * 6;
}
return getLastDayOfMonth(year, month);
}
/**
* 获取季度的最后一天(暂时没用)
*
* @param offset
* 偏移量
* @return
* @throws Exception
*/
public static String getLastDayOfQuarter(Date date) throws Exception {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.MONTH, 3);
int mon = cal.get(Calendar.MONTH);
if (mon >= Calendar.JANUARY && mon <= Calendar.MARCH) {
cal.set(Calendar.MONTH, Calendar.MARCH);
cal.set(Calendar.DAY_OF_MONTH, 31);
}
if (mon >= Calendar.APRIL && mon <= Calendar.JUNE) {
cal.set(Calendar.MONTH, Calendar.JUNE);
cal.set(Calendar.DAY_OF_MONTH, 30);
}
if (mon >= Calendar.JULY && mon <= Calendar.SEPTEMBER) {
cal.set(Calendar.MONTH, Calendar.SEPTEMBER);
cal.set(Calendar.DAY_OF_MONTH, 30);
}
if (mon >= Calendar.OCTOBER && mon <= Calendar.DECEMBER) {
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.DAY_OF_MONTH, 31);
}
return getStringDate(cal.getTime());
}
/**
* 返回所选类型日期段
*
* @param startDate_s
* 初始日期
* @param endDate_s
* 结束日期
* @param type
* 类型 1:月 2:季度 3:半年 4:年 5:周
* @return LinkedHashMap
* @throws Exception
*/
public static LinkedHashMap getDateType(String startDate_s, String endDate_s, int type) throws Exception {
if (compareTwoDateBigOrSmall1(endDate_s, startDate_s)) {// 如果传入的结尾日期小于开始日期,日期大小调换
String temp = startDate_s;
startDate_s = endDate_s;
endDate_s = temp;
}
Date startDate = switchStringToDate1(startDate_s);
Date endDate = switchStringToDate1(endDate_s);
int year = getYear(startDate);
if (year - getYear(endDate) != 0)
return null;// 如果年份不同,返回null
int startMonth = getMonth(startDate);// 初始月份
int endMonth = getMonth(endDate);// 结尾月份
// TreeMap map = new TreeMap();
LinkedHashMap map = new LinkedHashMap();
switch (type) {
case 1: // 按月
for (int m = startMonth; m <= endMonth; m++) {// 循环月份
// String s1 = year + "-" + m + "-" + "01";
String s1 = getStringDate(getFirstDayOfMonth(year, m));// 该月第一天
String s2 = getMonthLastDay(switchStringToDate1(s1));// 该月最后一天
if (m == startMonth) {
if (compareTwoDateBigOrSmall1(s1, startDate_s))// 传入初始日期后于该月第一天
s1 = startDate_s;
}
if (compareTwoDateBigOrSmall1(endDate_s, s2))// 该月最后一天后于传入结束日期
s2 = endDate_s;
map.put(m + "月", s1 + " " + s2);
}
break;
case 2: // 按季度
for (int m = startMonth; m <= endMonth; m++) {// 循环月份
int jd = 1;
if (m <= 3)
jd = 1;
else if (3 < m && m <= 6)
jd = 2;
else if (6 < m && m <= 9)
jd = 3;
else
jd = 4;
String s1 = getStringDate(getFirstDayOfQuarter(year, jd));// 该季度第一天
String s2 = getStringDate(getLastDayOfQuarter(year, jd));// 该季度最后一天
String sye = getStringDate(getLastDayOfMonth(year, m));// 该月最后一天
if (compareTwoDateBigOrSmall1(sye, s2)) {// 该季度最后一天后于该月最后一天
if (compareTwoDateBigOrSmall1(endDate_s, s2)) {// 该季度最后一天后于传入结束日期
if (compareTwoDateBigOrSmall1(s1, startDate_s))// 传入初始日期后于该季度第一天
s1 = startDate_s;
map.put(jd + "季度", s1 + " " + endDate_s);
}
continue;
} else {
if (compareTwoDateBigOrSmall1(s1, startDate_s))// 传入初始日期后于该季度第一天
s1 = startDate_s;
if (compareTwoDateBigOrSmall1(endDate_s, s2))// 该季度最后一天后于传入结束日期
s2 = endDate_s;
map.put(jd + "季度", s1 + " " + s2);
}
}
break;
case 3: // 按半年
for (int m = startMonth; m <= endMonth; m++) {// 循环月份
int bn = 1;
String bns = "";
if (m <= 6) {
bn = 1;
bns = "上";
} else {
bn = 2;
bns = "下";
}
String s1 = getStringDate(getFirstDayOfHalfYear(year, bn));// 该半年第一天
String s2 = getStringDate(getLastDayOfHalfYear(year, bn));// 该半年最后一天
String sye = getStringDate(getLastDayOfMonth(year, m));// 该月最后一天
if (compareTwoDateBigOrSmall1(sye, s2)) {// 该半年最后一天后于该月最后一天
if (compareTwoDateBigOrSmall1(endDate_s, s2)) {// 该半年最后一天后于传入结束日期
if (compareTwoDateBigOrSmall1(s1, startDate_s))// 传入初始日期后于该半年第一天
s1 = startDate_s;
map.put(bns + "半年", s1 + " " + endDate_s);
}
continue;
} else {
if (compareTwoDateBigOrSmall1(s1, startDate_s))// 传入初始日期后于该半年第一天
s1 = startDate_s;
if (compareTwoDateBigOrSmall1(endDate_s, s2))// 该半年最后一天后于传入结束日期
s2 = endDate_s;
map.put(bns + "半年", s1 + " " + s2);
}
}
break;
case 4: // 按年
map.put("年", startDate_s + " " + endDate_s);
break;
case 5: // 按周
int startWeek = getWeek(startDate);// 初始周数
int endWeek = getWeek(endDate);// 结尾周数
endWeek = getMaxDayWeek(year, endWeek, endMonth);
startWeek = getMaxDayWeek(year, startWeek, startMonth);
for (int m = startWeek; m <= endWeek; m++) {// 循环周数
String zs = String.valueOf(year).substring(2) + (("0" + String.valueOf(m)).substring(("0" + String.valueOf(m)).length() - 2));// 0805
String s1 = getStringDate(getWeekFirstDay(year, m));// 该周第一天
String s2 = getStringDate(getWeekLastDay(year, m));// 该周最后一天
if (compareTwoDateBigOrSmall1(endDate_s, s2)) {// 该周最后一天后于传入结束日期
if (compareTwoDateBigOrSmall1(s1, startDate_s))// 传入初始日期后于该周第一天
s1 = startDate_s;
map.put(zs, s1 + " " + endDate_s);
continue;
} else {
if (compareTwoDateBigOrSmall1(s1, startDate_s))// 传入初始日期后于该周第一天
s1 = startDate_s;
if (compareTwoDateBigOrSmall1(endDate_s, s2))// 该周最后一天后于传入结束日期
s2 = endDate_s;
map.put(zs, s1 + " " + s2);
}
}
break;
}
return map;
}
/**
* 返回所选类型日期段
*
* @param startDate_s
* 初始日期
* @param endDate_s
* 结束日期
* @param type
* 分析方式标志 1:年分析; 2: 季分析;3: 月分析;4:周分析;5:日分析
* @return LinkedHashMap
* @throws Exception
*/
public static LinkedHashMap getSepcDateType(String startDate_s, String endDate_s, int type) throws Exception {
if (compareTwoDateBigOrSmall1(endDate_s, startDate_s)) {// 如果传入的结尾日期小于开始日期,日期大小调换
String temp = startDate_s;
startDate_s = endDate_s;
endDate_s = temp;
}
Date startDate = switchStringToDate1(startDate_s);
Date endDate = switchStringToDate1(endDate_s);
int year = getYear(startDate);
if (year - getYear(endDate) != 0)
return null;// 如果年份不同,返回null
int startMonth = getMonth(startDate);// 初始月份
int endMonth = getMonth(endDate);// 结尾月份
// TreeMap map = new TreeMap();
LinkedHashMap map = new LinkedHashMap();
switch (type) {
case 1:// 按年
map.put("年", startDate_s + " " + endDate_s);
break;
case 2: // 按季度
for (int m = startMonth; m <= endMonth; m++) {// 循环月份
int jd = 1;
if (m <= 3)
jd = 1;
else if (3 < m && m <= 6)
jd = 2;
else if (6 < m && m <= 9)
jd = 3;
else
jd = 4;
String s1 = getStringDate(getFirstDayOfQuarter(year, jd));// 该季度第一天
String s2 = getStringDate(getLastDayOfQuarter(year, jd));// 该季度最后一天
String sye = getStringDate(getLastDayOfMonth(year, m));// 该月最后一天
if (compareTwoDateBigOrSmall1(sye, s2)) {// 该季度最后一天后于该月最后一天
if (compareTwoDateBigOrSmall1(endDate_s, s2)) {// 该季度最后一天后于传入结束日期
if (compareTwoDateBigOrSmall1(s1, startDate_s))// 传入初始日期后于该季度第一天
s1 = startDate_s;
map.put(jd + "季度", s1 + " " + endDate_s);
}
continue;
} else {
if (compareTwoDateBigOrSmall1(s1, startDate_s))// 传入初始日期后于该季度第一天
s1 = startDate_s;
if (compareTwoDateBigOrSmall1(endDate_s, s2))// 该季度最后一天后于传入结束日期
s2 = endDate_s;
map.put(jd + "季度", s1 + " " + s2);
}
}
break;
case 3:// 按月
for (int m = startMonth; m <= endMonth; m++) {// 循环月份
// String s1 = year + "-" + m + "-" + "01";
String s1 = getStringDate(getFirstDayOfMonth(year, m));// 该月第一天
String s2 = getMonthLastDay(switchStringToDate1(s1));// 该月最后一天
if (m == startMonth) {
if (compareTwoDateBigOrSmall1(s1, startDate_s))// 传入初始日期后于该月第一天
s1 = startDate_s;
}
if (compareTwoDateBigOrSmall1(endDate_s, s2))// 该月最后一天后于传入结束日期
s2 = endDate_s;
map.put(m + "月", s1 + " " + s2);
}
break;
case 4: // 按周
int startWeek = getWeek(startDate);// 初始周数
int endWeek = getWeek(endDate);// 结尾周数
endWeek = getMaxDayWeek(year, endWeek, endMonth);
startWeek = getMaxDayWeek(year, startWeek, startMonth);
for (int m = startWeek; m <= endWeek; m++) {// 循环周数
String zs = String.valueOf(year).substring(2) + (("0" + String.valueOf(m)).substring(("0" + String.valueOf(m)).length() - 2));
String s1 = getStringDate(getWeekFirstDay(year, m));// 该周第一天
String s2 = getStringDate(getWeekLastDay(year, m));// 该周最后一天
if (compareTwoDateBigOrSmall1(endDate_s, s2)) {// 该周最后一天后于传入结束日期
if (compareTwoDateBigOrSmall1(s1, startDate_s))// 传入初始日期后于该周第一天
s1 = startDate_s;
map.put(zs, s1 + " " + endDate_s); // 0802 年份周
continue;
} else {
if (compareTwoDateBigOrSmall1(s1, startDate_s))// 传入初始日期后于该周第一天
s1 = startDate_s;
if (compareTwoDateBigOrSmall1(endDate_s, s2))// 该周最后一天后于传入结束日期
s2 = endDate_s;
map.put(zs, s1 + " " + s2);
}
}
break;
case 5:// 按日
startMonth = getMonth(startDate);// 初始月份
endMonth = getMonth(endDate);// 结尾月份
int start = startMonth;
int end = endMonth;
for (int i = start; i <= end; i++) {// 循环月份
// 1)、对月份进行判断,并且遍历,整合出相应的日期组合
String specday = null; // 具体的某一天
String startDay = null;// 该月第一天
String endDay = null;// 该月最后一天
// 月份为2,判断是否为润年,并以月赋予日
if ((i == startMonth) && (i == endMonth)) {
startDay = startDate_s.split("-")[2];
endDay = endDate_s.split("-")[2];
} else if (i == startMonth) { // 选择的月份起始值
startDay = startDate_s.split("-")[2];// 该月选择起始日期
if (2 == i || "2".equals(i + "")) {
if (isLeapYear(year))
endDay = "29";
else
endDay = "28";
}
// 大月份
if ((1 == i) || (3 == i) || (5 == i) || (7 == i) || (8 == i) || (10 == i) || (12 == i)) {
endDay = "31";
}
// 小月份
if ((4 == i) || (6 == i) || (9 == i) || (11 == i)) {
endDay = "30";
}
} else if (i == endMonth) {// 选择的月末值
startDay = "1";
endDay = endDate_s.split("-")[2];
} else {
startDay = "1";
if (2 == i || "2".equals(i + "")) {
if (isLeapYear(year))
endDay = "29";
else
endDay = "28";
}
// 大月份
if ((1 == i) || (3 == i) || (5 == i) || (7 == i) || (8 == i) || (10 == i) || (12 == i)) {
endDay = "31";
}
// 小月份
if ((4 == i) || (6 == i) || (9 == i) || (11 == i)) {
endDay = "30";
}
}
if ((null != startDay) && (endDay != null)) {
int iterStartday = Integer.parseInt(startDay);
int iterEndday = Integer.parseInt(endDay);
for (int j = iterStartday; j <= iterEndday; j++) {
specday = year + "-" + i + "-"; // 具体的某一天
specday += j;
map.put(specday, specday);
}
}
}
break;
}
return map;
}
/**
* 返回相同年份所选类型日期段
*
* @param startDate_s
* 初始日期
* @param endDate_s
* 结束日期
* @param type
* 类型 1:月 2:季度 3:半年 4:年
* @return LinkedHashMap
* @throws Exception
*/
public static LinkedHashMap getSameYearDateType(String startDate, String endDate, int type) throws Exception {
String[] chooseStartDate = null;
chooseStartDate = startDate.split("~");
String[] chooseEndDate = null;
chooseEndDate = endDate.split("~");
String start = null;
String end = null;
if (chooseEndDate[0].compareToIgnoreCase(chooseStartDate[0]) < 0) {
start = chooseStartDate[0];
} else {
start = chooseEndDate[0];
}
if (chooseEndDate[1].compareToIgnoreCase(chooseStartDate[1]) > 0) {
end = chooseStartDate[1];
} else {
end = chooseEndDate[1];
}
return getDateType(start, end, type);
}
/**
* 返回相同年份所选类型日期段
*
* @param startDate_s
* 初始日期
* @param endDate_s
* 结束日期
* @param type
* 类型 1:年分析; 2: 季分析;3: 月分析;4:周分析;5:日分析
* @return LinkedHashMap
* @throws Exception
*/
public static LinkedHashMap getDateType(String startDate, int type) throws Exception {
String[] chooseDate = null;
chooseDate = startDate.split("~");
// chooseStartDate[0]:基准时间的起始日期;chooseStartDate[1]:基准时间的结束日期
// 比较:统计出合适的时间段
String start = null;
String end = null;
start = chooseDate[0];
end = chooseDate[1];
return getSepcDateType(start, end, type);
}
/**
* 判断此年份是否为润年
*
* @throws BaseAppException
*
*/
private static boolean isLeapYear(int yearReport) {
int year = yearReport;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true;
} else {
return false;
}
}
/**
* 判断字符串是否为yyyyMMdd格式的日期
*
* @param sDate
* 日期字符串,格式yyyyMMdd
* @return
*/
public static boolean isDate(String sDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
try {
if (sdf.format(sdf.parse(sDate)).equalsIgnoreCase(sDate))
return true;
} catch (Exception ex) {
System.out.println("Err:" + ex.getMessage());
}
return false;
}
/**
* 将一个日期字符串转化成日期
*
* @param sDate
* String
* @return Date yyyy-mm-dd
*/
public static Date switchStringToDate1_hd(String sDate) {
Date date = null;
if (sDate != null) {
if (sDate.trim().length() == 8) {// 字符串长度满足8
if (isDate(sDate.trim())) {// yyyyMMdd格式的字符串
sDate = sDate.substring(0, 4) + "-" + sDate.substring(4, 6) + "-" + sDate.substring(6, 8);
}
} else if (!"".equals(sDate.trim()) && sDate.length() > 1) {
if (sDate != "")
sDate = sDate.split(" ")[0];
String dot = "-";
if (sDate.indexOf(".") >= 0)
dot = ".";
int dy = sDate.indexOf(dot);// 第一个点位置
int dm = sDate.lastIndexOf(dot);// 第二个点位置
String sy = sDate.substring(0, dy);// 年
String sm = "0" + sDate.substring(dy + 1, dm);// 月
sm = sm.substring(sm.length() - 2);
String sd = "0" + sDate.substring(dm + 1);// 日
sd = sd.substring(sd.length() - 2);
// if(sds != null) {
// if(sds.length >= 3){
sDate = sy + "-" + sm + "-" + sd;
// }
// }
}
try {
if (!"".equals(sDate.trim()) && sDate.length() > 1) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
date = df.parse(sDate);
}
} catch (Exception e) {
log.error(new Date() + "日期转换失败:" + SystemUtil.errorMess(e));
//System.out.println("日期转换失败hd:" + e.getMessage());
}
}
return date;
}
/**
* 转化为有时分秒的格式
*
* @param sDate
* @return
*/
public static Date switchStringToDateHMS(String sDate) {
Date date = null;
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:24mi:ss");
date = df.parse(sDate);
} catch (Exception e) {
log.error(new Date() + "日期转换失败:" + SystemUtil.errorMess(e));
//System.out.println("日期转换失败hd:" + e.getMessage());
}
return date;
}
/**
* 传入字符串日期参数,返回此日期是这一年中的第几天
*
* @param String
* @return int
* */
public static int returnDayOfYear(String date) {
String time = date;
DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date d = null;
try {
d = f.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar c = Calendar.getInstance();
c.setTime(d);
return c.get(Calendar.DAY_OF_YEAR);
}
public static String returnFormatString(Date date) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String returnString = format.format(date);
return returnString;
}
/**
* 时间转化为xxxx年xx月xx日
*
* @param DateString
* yyyy-mm-dd
* @return
*/
public static String returnDateString1(String DateString) {
if (DateString != null && DateString.length() > 0) {
String[] time = DateString.split("-");
return time[0] + "年" + time[1] + "月" + time[2] + "日";
}
return "";
}
/**
* 返回xx时xx分
*/
public static String returnHM(String hour, String miniute) {
String h = null;
;
String m = null;
if (!"时".equals(hour) && null != hour && !"".equals(hour)) {
if (hour != null && hour.length() == 1) {
h = "0" + hour;
} else {
h = hour;
}
} else {
h = "00";
}
if (!"分".equals(miniute) && null != miniute && !"".equals(miniute)) {
if (miniute != null && miniute.length() == 1) {
m = "0" + miniute;
} else {
m = miniute;
}
} else {
m = "00";
}
return h + "时" + m + "分";
}
// /**
// * 返回"00"或其他时
// * @param HorM
// * @return
// */
// public static String returnH(String H){
// String accept = "00";
// if(!"时".equals(H)){
// accept = H;
// }
// return accept;
// }
// /**
// * 返回"00"或其他分值
// * @param HorM
// * @return
// */
// public static String returnM(String M){
// String accept = "00";
// if(!"分".equals(M)){
// accept = M;
// }
// return accept;
// }
/**
* 获取时间yyyy年mm月dd天hh时mm分
*
* @param time
* yyyy-mm-dd:hh:mm
* @return
*/
public static String returnTime(String time) {
String temp[] = time.split(":");
String returnTime = "";
if (temp.length >= 3) {
if (temp[1] != null) {
if (temp[1].length() == 1) {
temp[1] = "0" + temp[1];
}
}
if (temp[2] != null) {
if (temp[2].length() == 1) {
temp[2] = "0" + temp[2];
}
}
returnTime = returnDateString1(temp[0]) + " " + temp[1] + "时" + temp[2] + "分";
}
return returnTime;
}
/**
* 获取时间hh时mm分
*
* @param time
* hh:mm
* @return
*/
public static String returnTimeHM(String time) {
String temp[] = time.split(":");
String returnTime = "";
if (temp.length >= 2) {
returnTime = temp[0] + "时" + temp[1] + "分";
}
return returnTime;
}
/**
* 获取时,分集合
*
* @param temp
* @return
*/
public static List<String> getTimeList(String temp) {
List<String> timList = new ArrayList<String>();
if ("hour".equals(temp)) {
for (int i = 0; i <= 24; i++) {
if (i < 10) {
timList.add("0" + i);
} else {
timList.add(i + "");
}
}
}
if ("miniutes".equals(temp)) {
for (int i = 0; i < 60; i++) {
if (i < 10) {
timList.add("0" + i);
} else {
timList.add(i + "");
}
}
}
return timList;
}
/**
* 获取时,分集合
*
* @param temp
* @return
*/
public static List<String> getHourList(String temp) {
List<String> timList = new ArrayList<String>();
if ("hour".equals(temp)) {
for (int i = 0; i < 24; i++) {
if (i < 10) {
timList.add("0" + i);
} else {
timList.add(i + "");
}
}
}
if ("miniutes".equals(temp)) {
for (int i = 0; i < 60; i++) {
if (i < 10) {
timList.add("0" + i);
} else {
timList.add(i + "");
}
}
}
return timList;
}
public static String getReportDateTime(String dateTime) {
String reportDateTime = "";
if (dateTime != null && !"".equals(dateTime)) {
reportDateTime = returnDateString1(dateTime);
} else {
reportDateTime = " 年 月 日";
}
return reportDateTime;
}
/**
* 在制定日期后加N天、周、月、年
*
* @param time
* 日期
* @param n
* 加多少;正数为加,负数为减
* @param field
* 1:年;2:半年;3:一季度;4:一周;5:一天
* @return 格林时间,如Tue Oct 18 11:04:55 CST 2011
*/
public static Date getNDayAfterTime(Date time, int n, int field) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(time);
gc.add(field, n);
return gc.getTime();
}
/**
* 计算两个日期之间间隔,去除双休日
*
* @param startDate
* @param endDate
* @return
*/
public static int getDays(String startDate, String endDate) {
Date d1 = DateTimeUtil.switchStringToDate1(startDate);
Date d2 = DateTimeUtil.switchStringToDate1(endDate);
long l1 = d2.getTime() - d1.getTime();
int l2 = (int) Math.abs(l1 / (1000 * 24 * 60 * 60));
while (d1.before(d2)) {
if (d1.getDay() == 0 || d1.getDay() == 6) {
l2--;
}
d1 = DateTimeUtil.getNDayAfterTime(d1, 1, 5);
}
return l2;
}
/**
*
* @return 中文日期
*/
public static String getALlChineseStringDate(Date date) {
if (date == null)
return null;
SimpleDateFormat newk = new SimpleDateFormat("yyyy-MM-dd");
String str = newk.format(date);
String[] temp = str.split("-");
String year = temp[0];
String month = temp[1];
String day = temp[2];
String chineseYear = "";
String chineseMonth = "";
String chineseDay = "";
for (int i = 0; i < year.length(); i++) {
chineseYear += Chinese1[Integer.parseInt(year.substring(i, i + 1))];
}
chineseMonth = numberToChinese(month);
chineseDay = numberToChinese(day);
return chineseYear + "年" + chineseMonth + "月" + chineseDay+"日";
}
}
java的一些时间格式转换
最新推荐文章于 2020-09-26 16:01:53 发布