Commit 7c4f1a3c by 罗翻

utilsdate修改

parent b14540cc
/** /**
* CopyRight (C), 2012, www.winchannel.net * CopyRight (C), 2012, www.winchannel.net
*/ */
package com.dayu.bigfish.utils; package com.dayu.bigfish.utils;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.text.TextUtils; import android.text.TextUtils;
import java.text.ParseException; import java.text.ParseException;
import java.text.ParsePosition; import java.text.ParsePosition;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@SuppressLint("SimpleDateFormat") @SuppressLint("SimpleDateFormat")
public class UtilsDate { public class UtilsDate {
private static final String TAG = UtilsDate.class.getSimpleName(); private static final String TAG = UtilsDate.class.getSimpleName();
// 格式:年-月-日 小时:分钟:秒 // 格式:年-月-日 小时:分钟:秒
public static final String FORMAT_ONE = "yyyy-MM-dd HH:mm:ss"; public static final String FORMAT_ONE = "yyyy-MM-dd HH:mm:ss";
// 格式:年-月-日 小时:分钟 // 格式:年-月-日 小时:分钟
public static final String FORMAT_TWO = "yyyy-MM-dd HH:mm"; public static final String FORMAT_TWO = "yyyy-MM-dd HH:mm";
// 格式:年月日 小时分钟秒 // 格式:年月日 小时分钟秒
public static final String FORMAT_THREE = "yyyyMMdd-HHmmss"; public static final String FORMAT_THREE = "yyyyMMdd-HHmmss";
// 格式:月-日 小时:分钟 // 格式:月-日 小时:分钟
public static final String FORMAT_FOURTH = "MM-dd HH:mm"; public static final String FORMAT_FOURTH = "MM-dd HH:mm";
// 格式:年-月-日 // 格式:年-月-日
public static final String LONG_DATE_FORMAT = "yyyy-MM-dd"; public static final String LONG_DATE_FORMAT = "yyyy-MM-dd";
// 格式:月-日 // 格式:月-日
public static final String SHORT_DATE_FORMAT = "MM-dd"; public static final String SHORT_DATE_FORMAT = "MM-dd";
// 格式: 年月日时分秒 // 格式: 年月日时分秒
public static final String SHORT_LINE_FORMAT = "yyyyMMddHHmmss"; public static final String SHORT_LINE_FORMAT = "yyyyMMddHHmmss";
// 格式: 年月日时分秒 // 格式: 年月日时分秒
public static final String SHORT_LINE_FORMAT_TWO = "yyyyMMddHHmm"; public static final String SHORT_LINE_FORMAT_TWO = "yyyyMMddHHmm";
// 格式:小时:分钟:秒 // 格式:小时:分钟:秒
public static final String LONG_TIME_FORMAT = "HH:mm:ss"; public static final String LONG_TIME_FORMAT = "HH:mm:ss";
// 格式:小时:分钟 // 格式:小时:分钟
public static final String LONG_TIME_FORMAT_TWO = "HH:mm"; public static final String LONG_TIME_FORMAT_TWO = "HH:mm";
// 格式:年-月 // 格式:年-月
public static final String MONTG_DATE_FORMAT = "yyyy-MM"; public static final String MONTG_DATE_FORMAT = "yyyy-MM";
// 年的加减 // 年的加减
public static final int SUB_YEAR = Calendar.YEAR; public static final int SUB_YEAR = Calendar.YEAR;
// 月加减 // 月加减
public static final int SUB_MONTH = Calendar.MONTH; public static final int SUB_MONTH = Calendar.MONTH;
// 天的加减 // 天的加减
public static final int SUB_DAY = Calendar.DATE; public static final int SUB_DAY = Calendar.DATE;
// 小时的加减 // 小时的加减
public static final int SUB_HOUR = Calendar.HOUR; public static final int SUB_HOUR = Calendar.HOUR;
// 分钟的加减 // 分钟的加减
public static final int SUB_MINUTE = Calendar.MINUTE; public static final int SUB_MINUTE = Calendar.MINUTE;
// 秒的加减 // 秒的加减
public static final int SUB_SECOND = Calendar.SECOND; public static final int SUB_SECOND = Calendar.SECOND;
/** /**
* 把符合日期格式的字符串转换为日期类型. * 把符合日期格式的字符串转换为日期类型.
* *
* @param dateStr * @param dateStr
* @return * @return
*/ */
public static Date stringtoDate(String dateStr, String format) { public static Date stringtoDate(String dateStr, String format) {
Date d = null; Date d = null;
SimpleDateFormat formater = new SimpleDateFormat(format); SimpleDateFormat formater = new SimpleDateFormat(format);
try { try {
formater.setLenient(false); formater.setLenient(false);
d = formater.parse(dateStr); d = formater.parse(dateStr);
} catch (Exception e) { } catch (Exception e) {
// log.error(e); // log.error(e);
d = null; d = null;
} }
return d; return d;
} }
/** /**
* 把符合日期格式的字符串转换为日期类型. * 把符合日期格式的字符串转换为日期类型.
*/ */
public static Date stringtoDate(String dateStr, String format, public static Date stringtoDate(String dateStr, String format,
ParsePosition pos) { ParsePosition pos) {
Date d = null; Date d = null;
SimpleDateFormat formater = new SimpleDateFormat(format); SimpleDateFormat formater = new SimpleDateFormat(format);
try { try {
formater.setLenient(false); formater.setLenient(false);
d = formater.parse(dateStr, pos); d = formater.parse(dateStr, pos);
} catch (Exception e) { } catch (Exception e) {
d = null; d = null;
} }
return d; return d;
} }
/** /**
* 把日期转换为字符串. * 把日期转换为字符串.
* *
* @param date * @param date
* @return * @return
*/ */
public static String dateToString(Date date, String format) { public static String dateToString(Date date, String format) {
String result = ""; String result = "";
SimpleDateFormat formater = new SimpleDateFormat(format); SimpleDateFormat formater = new SimpleDateFormat(format);
try { try {
result = formater.format(date); result = formater.format(date);
} catch (Exception e) { } catch (Exception e) {
// log.error(e); // log.error(e);
} }
return result; return result;
} }
/** /**
* 获取当前时间的指定格式. * 获取当前时间的指定格式.
* *
* @param format * @param format
* @return * @return
*/ */
public static String getCurrDate(String format) { public static String getCurrDate(String format) {
return dateToString(new Date(), format); return dateToString(new Date(), format);
} }
/** /**
* @param dateStr * @param dateStr
* @param amount * @param amount
* @return * @return
*/ */
public static String dateSub(int dateKind, String dateStr, int amount) { public static String dateSub(int dateKind, String dateStr, int amount) {
Date date = stringtoDate(dateStr, FORMAT_ONE); Date date = stringtoDate(dateStr, FORMAT_ONE);
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTime(date); calendar.setTime(date);
calendar.add(dateKind, amount); calendar.add(dateKind, amount);
return dateToString(calendar.getTime(), FORMAT_ONE); return dateToString(calendar.getTime(), FORMAT_ONE);
} }
/** /**
* 两个日期相减 * 两个日期相减
* *
* @param firstTime * @param firstTime
* @param secTime * @param secTime
* @return 相减得到的秒数 * @return 相减得到的秒数
*/ */
public static long timeSub(String firstTime, String secTime) { public static long timeSub(String firstTime, String secTime) {
long first = stringtoDate(firstTime, FORMAT_ONE).getTime(); long first = stringtoDate(firstTime, FORMAT_ONE).getTime();
long second = stringtoDate(secTime, FORMAT_ONE).getTime(); long second = stringtoDate(secTime, FORMAT_ONE).getTime();
return (second - first) / 1000; return (second - first) / 1000;
} }
/** /**
* 获得某月的天数 * 获得某月的天数
* *
* @param year int * @param year int
* @param month int * @param month int
* @return int * @return int
*/ */
public static int getDaysOfMonth(String year, String month) { public static int getDaysOfMonth(String year, String month) {
int days = 0; int days = 0;
if (month.equals("1") || month.equals("3") || month.equals("5") if (month.equals("1") || month.equals("3") || month.equals("5")
|| month.equals("7") || month.equals("8") || month.equals("10") || month.equals("7") || month.equals("8") || month.equals("10")
|| month.equals("12")) { || month.equals("12")) {
days = 31; days = 31;
} else if (month.equals("4") || month.equals("6") || month.equals("9") } else if (month.equals("4") || month.equals("6") || month.equals("9")
|| month.equals("11")) { || month.equals("11")) {
days = 30; days = 30;
} else { } else {
if ((Integer.parseInt(year) % 4 == 0 && Integer.parseInt(year) % 100 != 0) if ((Integer.parseInt(year) % 4 == 0 && Integer.parseInt(year) % 100 != 0)
|| Integer.parseInt(year) % 400 == 0) { || Integer.parseInt(year) % 400 == 0) {
days = 29; days = 29;
} else { } else {
days = 28; days = 28;
} }
} }
return days; return days;
} }
/** /**
* 获取某年某月的天数 * 获取某年某月的天数
* *
* @param year int * @param year int
* @param month int 月份[1-12] * @param month int 月份[1-12]
* @return int * @return int
*/ */
public static int getDaysOfMonth(int year, int month) { public static int getDaysOfMonth(int year, int month) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1); calendar.set(year, month - 1, 1);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
} }
/** /**
* 获得当前日期(几号) * 获得当前日期(几号)
* *
* @return int * @return int
*/ */
public static int getToday() { public static int getToday() {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.DATE); return calendar.get(Calendar.DATE);
} }
/** /**
* 获得当前月份 * 获得当前月份
* *
* @return int * @return int
*/ */
public static int getToMonth() { public static int getToMonth() {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.MONTH) + 1; return calendar.get(Calendar.MONTH) + 1;
} }
/** /**
* 获得当前年份 * 获得当前年份
* *
* @return int * @return int
*/ */
public static int getToYear() { public static int getToYear() {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.YEAR); return calendar.get(Calendar.YEAR);
} }
/** /**
* 返回日期的天 * 返回日期的天
* *
* @param date Date * @param date Date
* @return int * @return int
*/ */
public static int getDay(Date date) { public static int getDay(Date date) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTime(date); calendar.setTime(date);
return calendar.get(Calendar.DATE); return calendar.get(Calendar.DATE);
} }
/** /**
* 返回日期的年 * 返回日期的年
* *
* @param date Date * @param date Date
* @return int * @return int
*/ */
public static int getYear(Date date) { public static int getYear(Date date) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTime(date); calendar.setTime(date);
return calendar.get(Calendar.YEAR); return calendar.get(Calendar.YEAR);
} }
/** /**
* 返回日期的月份,1-12 * 返回日期的月份,1-12
* *
* @param date Date * @param date Date
* @return int * @return int
*/ */
public static int getMonth(Date date) { public static int getMonth(Date date) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTime(date); calendar.setTime(date);
return calendar.get(Calendar.MONTH) + 1; return calendar.get(Calendar.MONTH) + 1;
} }
/** /**
* 计算两个日期相差的天数,如果date2 > date1 返回正数,否则返回负数 * 计算两个日期相差的天数,如果date2 > date1 返回正数,否则返回负数
* *
* @param date1 Date * @param date1 Date
* @param date2 Date * @param date2 Date
* @return long * @return long
*/ */
public static long dayDiff(Date date1, Date date2) { public static long dayDiff(Date date1, Date date2) {
return (date2.getTime() - date1.getTime()) / 86400000; return (date2.getTime() - date1.getTime()) / 86400000;
} }
/** /**
* 比较两个日期的年差 * 比较两个日期的年差
* *
* @param befor * @param befor
* @param after * @param after
* @return * @return
*/ */
public static int yearDiff(String before, String after) { public static int yearDiff(String before, String after) {
Date beforeDay = stringtoDate(before, LONG_DATE_FORMAT); Date beforeDay = stringtoDate(before, LONG_DATE_FORMAT);
Date afterDay = stringtoDate(after, LONG_DATE_FORMAT); Date afterDay = stringtoDate(after, LONG_DATE_FORMAT);
return getYear(afterDay) - getYear(beforeDay); return getYear(afterDay) - getYear(beforeDay);
} }
/** /**
* 比较指定日期与当前日期的差 * 比较指定日期与当前日期的差
* *
* @param befor * @param befor
* @param after * @param after
* @return * @return
*/ */
public static int yearDiffCurr(String after) { public static int yearDiffCurr(String after) {
Date beforeDay = new Date(); Date beforeDay = new Date();
Date afterDay = stringtoDate(after, LONG_DATE_FORMAT); Date afterDay = stringtoDate(after, LONG_DATE_FORMAT);
return getYear(beforeDay) - getYear(afterDay); return getYear(beforeDay) - getYear(afterDay);
} }
/** /**
* 比较指定日期与当前日期的差 * 比较指定日期与当前日期的差
* *
* @param before * @param before
* @return * @return
* @author chenyz * @author chenyz
*/ */
public static long dayDiffCurr(String before) { public static long dayDiffCurr(String before) {
Date currDate = UtilsDate.stringtoDate(currDay(), LONG_DATE_FORMAT); Date currDate = UtilsDate.stringtoDate(currDay(), LONG_DATE_FORMAT);
Date beforeDate = stringtoDate(before, LONG_DATE_FORMAT); Date beforeDate = stringtoDate(before, LONG_DATE_FORMAT);
return (currDate.getTime() - beforeDate.getTime()) / 86400000; return (currDate.getTime() - beforeDate.getTime()) / 86400000;
} }
public static boolean isDateOneBigger(String str1, String str2) { public static boolean isDateOneBigger(String str1, String str2) {
boolean isBigger = false; boolean isBigger = false;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dt1 = null; Date dt1 = null;
Date dt2 = null; Date dt2 = null;
try { try {
dt1 = sdf.parse(str1); dt1 = sdf.parse(str1);
dt2 = sdf.parse(str2); dt2 = sdf.parse(str2);
} catch (ParseException e) { } catch (ParseException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (dt1.getTime() > dt2.getTime()) { if (dt1.getTime() > dt2.getTime()) {
isBigger = true; isBigger = true;
} else if (dt1.getTime() < dt2.getTime()) { } else if (dt1.getTime() < dt2.getTime()) {
isBigger = false; isBigger = false;
} }
return isBigger; return isBigger;
} }
/** /**
* judge if in the time interval. * judge if in the time interval.
* *
* @param time time. * @param time time.
* @param startTime startTime. * @param startTime startTime.
* @param endTime endTime. * @param endTime endTime.
* @return * @return
*/ */
public static boolean isInTimeInterval(String time, String startTime, String endTime) { public static boolean isInTimeInterval(String time, String startTime, String endTime) {
String[] timeStrs = time.split(":"); String[] timeStrs = time.split(":");
String[] startTimeStrs = startTime.split(":"); String[] startTimeStrs = startTime.split(":");
String[] endTimeStrs = endTime.split(":"); String[] endTimeStrs = endTime.split(":");
int timeSecondNum = 0; int timeSecondNum = 0;
if (timeStrs.length == 2) { if (timeStrs.length == 2) {
timeSecondNum = Integer.parseInt(timeStrs[0]) * 60 * 60 + Integer.parseInt(timeStrs[1]) * 60; timeSecondNum = Integer.parseInt(timeStrs[0]) * 60 * 60 + Integer.parseInt(timeStrs[1]) * 60;
} else if (timeStrs.length == 3) { } else if (timeStrs.length == 3) {
timeSecondNum = Integer.parseInt(timeStrs[0]) * 60 * 60 + Integer.parseInt(timeStrs[1]) * 60 + timeSecondNum = Integer.parseInt(timeStrs[0]) * 60 * 60 + Integer.parseInt(timeStrs[1]) * 60 +
Integer.parseInt(timeStrs[2]); Integer.parseInt(timeStrs[2]);
} }
int startTimeSecondNum = 0; int startTimeSecondNum = 0;
if (startTimeStrs.length == 2) { if (startTimeStrs.length == 2) {
startTimeSecondNum = Integer.parseInt(startTimeStrs[0]) * 60 * 60 + Integer.parseInt(startTimeStrs[1]) * 60; startTimeSecondNum = Integer.parseInt(startTimeStrs[0]) * 60 * 60 + Integer.parseInt(startTimeStrs[1]) * 60;
} else if (startTimeStrs.length == 3) { } else if (startTimeStrs.length == 3) {
startTimeSecondNum = Integer.parseInt(startTimeStrs[0]) * 60 * 60 + Integer.parseInt(startTimeStrs[1]) * 60 + startTimeSecondNum = Integer.parseInt(startTimeStrs[0]) * 60 * 60 + Integer.parseInt(startTimeStrs[1]) * 60 +
Integer.parseInt(startTimeStrs[2]); Integer.parseInt(startTimeStrs[2]);
} }
int endTimeSecondNum = 0; int endTimeSecondNum = 0;
if (endTimeStrs.length == 2) { if (endTimeStrs.length == 2) {
endTimeSecondNum = Integer.parseInt(endTimeStrs[0]) * 60 * 60 + Integer.parseInt(endTimeStrs[1]) * 60; endTimeSecondNum = Integer.parseInt(endTimeStrs[0]) * 60 * 60 + Integer.parseInt(endTimeStrs[1]) * 60;
} else if (endTimeStrs.length == 3) { } else if (endTimeStrs.length == 3) {
endTimeSecondNum = Integer.parseInt(endTimeStrs[0]) * 60 * 60 + Integer.parseInt(endTimeStrs[1]) * 60 + endTimeSecondNum = Integer.parseInt(endTimeStrs[0]) * 60 * 60 + Integer.parseInt(endTimeStrs[1]) * 60 +
Integer.parseInt(endTimeStrs[2]); Integer.parseInt(endTimeStrs[2]);
} }
if (endTimeSecondNum > startTimeSecondNum) { if (endTimeSecondNum > startTimeSecondNum) {
return timeSecondNum >= startTimeSecondNum && timeSecondNum < endTimeSecondNum; return timeSecondNum >= startTimeSecondNum && timeSecondNum < endTimeSecondNum;
} else { } else {
return timeSecondNum < endTimeSecondNum || timeSecondNum >= startTimeSecondNum; return timeSecondNum < endTimeSecondNum || timeSecondNum >= startTimeSecondNum;
} }
} }
/** /**
* 获取每月的第一周. * 获取每月的第一周.
* *
* @param year * @param year
* @param month * @param month
* @return * @return
* @author chenyz * @author chenyz
*/ */
public static int getFirstWeekdayOfMonth(int year, int month) { public static int getFirstWeekdayOfMonth(int year, int month) {
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.SATURDAY); // 星期天为第一天 c.setFirstDayOfWeek(Calendar.SATURDAY); // 星期天为第一天
c.set(year, month - 1, 1); c.set(year, month - 1, 1);
return c.get(Calendar.DAY_OF_WEEK); return c.get(Calendar.DAY_OF_WEEK);
} }
/** /**
* 获取每月的最后一周. * 获取每月的最后一周.
* *
* @param year * @param year
* @param month * @param month
* @return * @return
* @author chenyz * @author chenyz
*/ */
public static int getLastWeekdayOfMonth(int year, int month) { public static int getLastWeekdayOfMonth(int year, int month) {
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.SATURDAY); // 星期天为第一天 c.setFirstDayOfWeek(Calendar.SATURDAY); // 星期天为第一天
c.set(year, month - 1, getDaysOfMonth(year, month)); c.set(year, month - 1, getDaysOfMonth(year, month));
return c.get(Calendar.DAY_OF_WEEK); return c.get(Calendar.DAY_OF_WEEK);
} }
/** /**
* 获得当前日期字符串,格式"yyyy-MM-dd HH:mm:ss". * 获得当前日期字符串,格式"yyyy-MM-dd HH:mm:ss".
* *
* @return * @return
*/ */
public static String getNow() { public static String getNow() {
Calendar today = Calendar.getInstance(); Calendar today = Calendar.getInstance();
return dateToString(today.getTime(), FORMAT_ONE); return dateToString(today.getTime(), FORMAT_ONE);
} }
public static Date getNowDate() { public static Date getNowDate() {
return Calendar.getInstance().getTime(); return Calendar.getInstance().getTime();
} }
/** /**
* 根据生日获取星座 * 根据生日获取星座
* *
* @param birth YYYY-mm-dd * @param birth YYYY-mm-dd
* @return * @return
*/ */
public static String getAstro(String birth) { public static String getAstro(String birth) {
if (!isDate(birth)) { if (!isDate(birth)) {
birth = "2000" + birth; birth = "2000" + birth;
} }
if (!isDate(birth)) { if (!isDate(birth)) {
return ""; return "";
} }
int month = Integer.parseInt(birth.substring(birth.indexOf("-") + 1, int month = Integer.parseInt(birth.substring(birth.indexOf("-") + 1,
birth.lastIndexOf("-"))); birth.lastIndexOf("-")));
int day = Integer.parseInt(birth.substring(birth.lastIndexOf("-") + 1)); int day = Integer.parseInt(birth.substring(birth.lastIndexOf("-") + 1));
String s = "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯"; String s = "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
int[] arr = { int[] arr = {
20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22
}; };
int start = month * 2 - (day < arr[month - 1] ? 2 : 0); int start = month * 2 - (day < arr[month - 1] ? 2 : 0);
return s.substring(start, start + 2) + "座"; return s.substring(start, start + 2) + "座";
} }
/** /**
* 判断日期是否有效,包括闰年的情况 * 判断日期是否有效,包括闰年的情况
* *
* @param date YYYY-mm-dd * @param date YYYY-mm-dd
* @return * @return
*/ */
public static boolean isDate(String date) { public static boolean isDate(String date) {
StringBuffer reg = new StringBuffer( StringBuffer reg = new StringBuffer(
"^((\\d{2}(([02468][048])|([13579][26]))-?((((0?"); "^((\\d{2}(([02468][048])|([13579][26]))-?((((0?");
reg.append("[13578])|(1[02]))-?((0?[1-9])|([1-2][0-9])|(3[01])))"); reg.append("[13578])|(1[02]))-?((0?[1-9])|([1-2][0-9])|(3[01])))");
reg.append("|(((0?[469])|(11))-?((0?[1-9])|([1-2][0-9])|(30)))|"); reg.append("|(((0?[469])|(11))-?((0?[1-9])|([1-2][0-9])|(30)))|");
reg.append("(0?2-?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][12"); reg.append("(0?2-?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][12");
reg.append("35679])|([13579][01345789]))-?((((0?[13578])|(1[02]))"); reg.append("35679])|([13579][01345789]))-?((((0?[13578])|(1[02]))");
reg.append("-?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))"); reg.append("-?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))");
reg.append("-?((0?[1-9])|([1-2][0-9])|(30)))|(0?2-?((0?["); reg.append("-?((0?[1-9])|([1-2][0-9])|(30)))|(0?2-?((0?[");
reg.append("1-9])|(1[0-9])|(2[0-8]))))))"); reg.append("1-9])|(1[0-9])|(2[0-8]))))))");
Pattern p = Pattern.compile(reg.toString()); Pattern p = Pattern.compile(reg.toString());
return p.matcher(date).matches(); return p.matcher(date).matches();
} }
/** /**
* 取得指定日期过 months 月后的日期 (当 months 为负数表示指定月之前); * 取得指定日期过 months 月后的日期 (当 months 为负数表示指定月之前);
* *
* @param date 日期 为null时表示当天 * @param date 日期 为null时表示当天
* @param month 相加(相减)的月数 * @param month 相加(相减)的月数
*/ */
public static Date nextMonth(Date date, int months) { public static Date nextMonth(Date date, int months) {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
if (date != null) { if (date != null) {
cal.setTime(date); cal.setTime(date);
} }
cal.add(Calendar.MONTH, months); cal.add(Calendar.MONTH, months);
return cal.getTime(); return cal.getTime();
} }
/** /**
* 取得指定日期过 day 天后的日期 (当 day 为负数表示指日期之前); * 取得指定日期过 day 天后的日期 (当 day 为负数表示指日期之前);
* *
* @param date 日期 为null时表示当天 * @param date 日期 为null时表示当天
* @param month 相加(相减)的月数 * @param month 相加(相减)的月数
*/ */
public static Date nextDay(Date date, int day) { public static Date nextDay(Date date, int day) {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
if (date != null) { if (date != null) {
cal.setTime(date); cal.setTime(date);
} }
cal.add(Calendar.DAY_OF_YEAR, day); cal.add(Calendar.DAY_OF_YEAR, day);
return cal.getTime(); return cal.getTime();
} }
/** /**
* 取得距离今天 day 日的日期 * 取得距离今天 day 日的日期
* *
* @param day * @param day
* @param format * @param format
* @return * @return
* @author chenyz * @author chenyz
*/ */
public static String nextDay(int day, String format) { public static String nextDay(int day, String format) {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.setTime(new Date()); cal.setTime(new Date());
cal.add(Calendar.DAY_OF_YEAR, day); cal.add(Calendar.DAY_OF_YEAR, day);
return dateToString(cal.getTime(), format); return dateToString(cal.getTime(), format);
} }
/** /**
* 取得指定日期过 day 周后的日期 (当 day 为负数表示指定月之前) * 取得指定日期过 day 周后的日期 (当 day 为负数表示指定月之前)
* *
* @param date 日期 为null时表示当天 * @param date 日期 为null时表示当天
*/ */
public static Date nextWeek(Date date, int week) { public static Date nextWeek(Date date, int week) {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
if (date != null) { if (date != null) {
cal.setTime(date); cal.setTime(date);
} }
cal.add(Calendar.WEEK_OF_MONTH, week); cal.add(Calendar.WEEK_OF_MONTH, week);
return cal.getTime(); return cal.getTime();
} }
/** /**
* 获取当前的日期(yyyy-MM-dd) * 获取当前的日期(yyyy-MM-dd)
*/ */
public static String currDay() { public static String currDay() {
return UtilsDate.dateToString(new Date(), UtilsDate.LONG_DATE_FORMAT); return UtilsDate.dateToString(new Date(), UtilsDate.LONG_DATE_FORMAT);
} }
/** /**
* 获取昨天的日期 * 获取昨天的日期
* *
* @return * @return
*/ */
public static String befoDay() { public static String befoDay() {
return befoDay(UtilsDate.LONG_DATE_FORMAT); return befoDay(UtilsDate.LONG_DATE_FORMAT);
} }
/** /**
* 根据时间类型获取昨天的日期 * 根据时间类型获取昨天的日期
* *
* @param format * @param format
* @return * @return
* @author chenyz * @author chenyz
*/ */
public static String befoDay(String format) { public static String befoDay(String format) {
return UtilsDate.dateToString(UtilsDate.nextDay(new Date(), -1), format); return UtilsDate.dateToString(UtilsDate.nextDay(new Date(), -1), format);
} }
/** /**
* 获取明天的日期 * 获取明天的日期
*/ */
public static String afterDay() { public static String afterDay() {
return UtilsDate.dateToString(UtilsDate.nextDay(new Date(), 1), return UtilsDate.dateToString(UtilsDate.nextDay(new Date(), 1),
UtilsDate.LONG_DATE_FORMAT); UtilsDate.LONG_DATE_FORMAT);
} }
/** /**
* 取得当前时间距离1900/1/1的天数 * 取得当前时间距离1900/1/1的天数
* *
* @return * @return
*/ */
public static int getDayNum() { public static int getDayNum() {
int daynum = 0; int daynum = 0;
GregorianCalendar gd = new GregorianCalendar(); GregorianCalendar gd = new GregorianCalendar();
Date dt = gd.getTime(); Date dt = gd.getTime();
GregorianCalendar gd1 = new GregorianCalendar(1900, 1, 1); GregorianCalendar gd1 = new GregorianCalendar(1900, 1, 1);
Date dt1 = gd1.getTime(); Date dt1 = gd1.getTime();
daynum = (int) ((dt.getTime() - dt1.getTime()) / (24 * 60 * 60 * 1000)); daynum = (int) ((dt.getTime() - dt1.getTime()) / (24 * 60 * 60 * 1000));
return daynum; return daynum;
} }
/** /**
* getDayNum的逆方法(用于处理Excel取出的日期格式数据等) * getDayNum的逆方法(用于处理Excel取出的日期格式数据等)
* *
* @param day * @param day
* @return * @return
*/ */
public static Date getDateByNum(int day) { public static Date getDateByNum(int day) {
GregorianCalendar gd = new GregorianCalendar(1900, 1, 1); GregorianCalendar gd = new GregorianCalendar(1900, 1, 1);
Date date = gd.getTime(); Date date = gd.getTime();
date = nextDay(date, day); date = nextDay(date, day);
return date; return date;
} }
/** /**
* 针对yyyy-MM-dd HH:mm:ss格式,显示yyyymmdd * 针对yyyy-MM-dd HH:mm:ss格式,显示yyyymmdd
*/ */
public static String getYmdDateCN(String datestr) { public static String getYmdDateCN(String datestr) {
if (datestr == null) if (datestr == null)
return ""; return "";
if (datestr.length() < 10) if (datestr.length() < 10)
return ""; return "";
StringBuffer buf = new StringBuffer(); StringBuffer buf = new StringBuffer();
buf.append(datestr.substring(0, 4)).append(datestr.substring(5, 7)) buf.append(datestr.substring(0, 4)).append(datestr.substring(5, 7))
.append(datestr.substring(8, 10)); .append(datestr.substring(8, 10));
return buf.toString(); return buf.toString();
} }
/** /**
* 获取本月第一天 * 获取本月第一天
* *
* @param format * @param format
* @return * @return
*/ */
public static String getFirstDayOfMonth(String format) { public static String getFirstDayOfMonth(String format) {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 1); cal.set(Calendar.DATE, 1);
return dateToString(cal.getTime(), format); return dateToString(cal.getTime(), format);
} }
/** /**
* 获取本月最后一天 * 获取本月最后一天
* *
* @param format * @param format
* @return * @return
*/ */
public static String getLastDayOfMonth(String format) { public static String getLastDayOfMonth(String format) {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 1); cal.set(Calendar.DATE, 1);
cal.add(Calendar.MONTH, 1); cal.add(Calendar.MONTH, 1);
cal.add(Calendar.DATE, -1); cal.add(Calendar.DATE, -1);
return dateToString(cal.getTime(), format); return dateToString(cal.getTime(), format);
} }
/** /**
* 获取yyyy年mm月dd日 * 获取yyyy年mm月dd日
* *
* @param date * @param date
* @return * @return
*/ */
public static String getChineDate(Date date) { public static String getChineDate(Date date) {
String strDate = UtilsDate.dateToString(date, UtilsDate.LONG_DATE_FORMAT); String strDate = UtilsDate.dateToString(date, UtilsDate.LONG_DATE_FORMAT);
return strDate.split("-")[0] + "年" + strDate.split("-")[1] + "月" + strDate.split("-")[2] return strDate.split("-")[0] + "年" + strDate.split("-")[1] + "月" + strDate.split("-")[2]
+ "日"; + "日";
} }
/** /**
* 获得yyyy年MM月dd日 hh:mm:ss * 获得yyyy年MM月dd日 hh:mm:ss
* *
* @param date * @param date
* @return * @return
*/ */
public static String getChineLongDate(Date date) { public static String getChineLongDate(Date date) {
String strDate = UtilsDate.dateToString(date, UtilsDate.LONG_DATE_FORMAT); String strDate = UtilsDate.dateToString(date, UtilsDate.LONG_DATE_FORMAT);
return strDate.split("-")[0] + "年" + strDate.split("-")[1] + "月" + strDate.split("-")[2] return strDate.split("-")[0] + "年" + strDate.split("-")[1] + "月" + strDate.split("-")[2]
+ "日" + "日"
+ " " + dateToString(date, LONG_TIME_FORMAT); + " " + dateToString(date, LONG_TIME_FORMAT);
} }
/** /**
* 获取mm月dd日 hh:mm:ss * 获取mm月dd日 hh:mm:ss
* *
* @param date * @param date
* @return * @return
*/ */
public static String getChineShortDate(Date date) { public static String getChineShortDate(Date date) {
String strDate = UtilsDate.dateToString(date, UtilsDate.LONG_DATE_FORMAT); String strDate = UtilsDate.dateToString(date, UtilsDate.LONG_DATE_FORMAT);
return strDate.split("-")[1] + "月" + strDate.split("-")[2] + "日" return strDate.split("-")[1] + "月" + strDate.split("-")[2] + "日"
+ " " + dateToString(date, LONG_TIME_FORMAT); + " " + dateToString(date, LONG_TIME_FORMAT);
} }
public static String getyyyMMddHHmmss2(long ms) { public static String getyyyMMddHHmmss2(long ms) {
SimpleDateFormat mFormat = new SimpleDateFormat("yyyyMMdd_HHmmss"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
Date date = new Date(ms); Date date = new Date(ms);
String string = mFormat.format(date); String string = mFormat.format(date);
return string; return string;
} }
/** /**
* 得到年月日时分秒 * 得到年月日时分秒
*/ */
public static String getyyyyMMddHHmmss(long ms) { public static String getyyyyMMddHHmmss(long ms) {
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(ms); Date date = new Date(ms);
String string = mFormat.format(date); String string = mFormat.format(date);
return string; return string;
} }
/** /**
* 得到年月日 * 得到年月日
*/ */
public static String getyyyyMMdd(long ms) { public static String getyyyyMMdd(long ms) {
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(ms); Date date = new Date(ms);
String string = mFormat.format(date); String string = mFormat.format(date);
return string; return string;
} }
public static Date getyyyyMMddHHmmss(String str) { public static Date getyyyyMMddHHmmss(String str) {
if (!TextUtils.isEmpty(str)) { if (!TextUtils.isEmpty(str)) {
try { try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(str); Date date = sdf.parse(str);
return date; return date;
} catch (ParseException e) { } catch (ParseException e) {
} }
} }
return null; return null;
} }
/** /**
* 得到时分秒 * 得到时分秒
*/ */
public static String getHHmmss(long ms) { public static String getHHmmss(long ms) {
SimpleDateFormat mFormat = new SimpleDateFormat("HH:mm:ss"); SimpleDateFormat mFormat = new SimpleDateFormat("HH:mm:ss");
Date date = new Date(ms); Date date = new Date(ms);
String string = mFormat.format(date); String string = mFormat.format(date);
return string; return string;
} }
/** /**
* 用来计算两个long型之差的时分秒 * 用来计算两个long型之差的时分秒
*/ */
public static String getHHmmssBetween(long ms) { public static String getHHmmssBetween(long ms) {
long second = ms / 1000 % 60; long second = ms / 1000 % 60;
long minute = ms / 1000 / 60 % 60; long minute = ms / 1000 / 60 % 60;
long hour = ms / 1000 / 60 / 60; long hour = ms / 1000 / 60 / 60;
String secondStr = String.valueOf(second); String secondStr = String.valueOf(second);
String minuteStr = String.valueOf(minute); String minuteStr = String.valueOf(minute);
String hourStr = String.valueOf(hour); String hourStr = String.valueOf(hour);
StringBuilder mSB = new StringBuilder(); StringBuilder mSB = new StringBuilder();
mSB.append(hourStr.length() < 2 ? "0" + hourStr : hourStr).append(":"); mSB.append(hourStr.length() < 2 ? "0" + hourStr : hourStr).append(":");
mSB.append(minuteStr.length() < 2 ? "0" + minuteStr : minuteStr).append(":"); mSB.append(minuteStr.length() < 2 ? "0" + minuteStr : minuteStr).append(":");
mSB.append(secondStr.length() < 2 ? "0" + secondStr : secondStr); mSB.append(secondStr.length() < 2 ? "0" + secondStr : secondStr);
return mSB.toString(); return mSB.toString();
} }
public static long getLongFromFormat(String date, String format) { public static long getLongFromFormat(String date, String format) {
SimpleDateFormat mFormat = new SimpleDateFormat(format); SimpleDateFormat mFormat = new SimpleDateFormat(format);
Date parse = null; Date parse = null;
try { try {
parse = mFormat.parse(date); parse = mFormat.parse(date);
} catch (ParseException e) { } catch (ParseException e) {
} }
if (parse != null) { if (parse != null) {
return parse.getTime(); return parse.getTime();
} else { } else {
return -1; return -1;
} }
} }
public static long getLongFromyyyyMMdd(String date) { public static long getLongFromyyyyMMdd(String date) {
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parse = null; Date parse = null;
try { try {
parse = mFormat.parse(date); parse = mFormat.parse(date);
} catch (ParseException e) { } catch (ParseException e) {
} }
if (parse != null) { if (parse != null) {
return parse.getTime(); return parse.getTime();
} else { } else {
return -1; return -1;
} }
} }
public static String getDateStringFromyyyyMMdd(String date) { public static String getDateStringFromyyyyMMdd(String date) {
// String year = date.substring(0, 4); // String year = date.substring(0, 4);
// String month = date.substring(4,6); // String month = date.substring(4,6);
// String day = date.substring(6,8); // String day = date.substring(6,8);
// return year+ "-" + month + "-" + day; // return year+ "-" + month + "-" + day;
// return year + month + day; // return year + month + day;
return date; return date;
} }
/** /**
* yyyy-MM-dd HH:mm:ss * yyyy-MM-dd HH:mm:ss
*/ */
public static long getLongFromyyyyMMddHHmmss(String date) { public static long getLongFromyyyyMMddHHmmss(String date) {
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = null; Date parse = null;
try { try {
parse = mFormat.parse(date); parse = mFormat.parse(date);
} catch (ParseException e) { } catch (ParseException e) {
} }
if (parse != null) { if (parse != null) {
return parse.getTime(); return parse.getTime();
} else { } else {
return -1; return -1;
} }
} }
/** /**
* yyyy-MM-dd HH:mm:ss * yyyy-MM-dd HH:mm:ss
*/ */
public static long getLongFromyyyyMMddHHmmss_ext(String date) { public static long getLongFromyyyyMMddHHmmss_ext(String date) {
SimpleDateFormat mFormat = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date parse = null; Date parse = null;
try { try {
parse = mFormat.parse(date); parse = mFormat.parse(date);
} catch (ParseException e) { } catch (ParseException e) {
} }
if (parse != null) { if (parse != null) {
return parse.getTime(); return parse.getTime();
} else { } else {
return -1; return -1;
} }
} }
// for winstat start // for winstat start
/** /**
* 得到年月日时分秒 * 得到年月日时分秒
*/ */
public static String getTimeStamps() { public static String getTimeStamps() {
long ms = System.currentTimeMillis(); long ms = System.currentTimeMillis();
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
Date date = new Date(ms); Date date = new Date(ms);
String string = mFormat.format(date); String string = mFormat.format(date);
return string; return string;
} }
/** /**
* 得到年月日时分秒 * 得到年月日时分秒
*/ */
public static String getyyyyMMddHHmmss_f1(long ms) { public static String getyyyyMMddHHmmss_f1(long ms) {
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
Date date = new Date(ms); Date date = new Date(ms);
String string = mFormat.format(date); String string = mFormat.format(date);
return string; return string;
} }
/** /**
* yyyy-MM-dd HH:mm:ssZ * yyyy-MM-dd HH:mm:ssZ
*/ */
public static String getLongFromyyyyMMddHHmmssZ(long ms) { public static String getLongFromyyyyMMddHHmmssZ(long ms) {
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
Date date = new Date(ms); Date date = new Date(ms);
String string = mFormat.format(date); String string = mFormat.format(date);
return string; return string;
} }
/** /**
* 得到年月日时分秒 * 得到年月日时分秒
*/ */
public static String getyyyyMMddHHmmss_f2(long ms) { public static String getyyyyMMddHHmmss_f2(long ms) {
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = new Date(ms); Date date = new Date(ms);
String string = mFormat.format(date); String string = mFormat.format(date);
return string; return string;
} }
// end for winstat // end for winstat
/** /**
* 计算两个日期之间相差的天数 * 计算两个日期之间相差的天数
* *
* @param smdate 较小的时间 * @param smdate 较小的时间
* @param bdate 较大的时间 * @param bdate 较大的时间
* @return 相差天数 * @return 相差天数
* @throws ParseException * @throws ParseException
*/ */
public static int daysBetween(Date smdate, Date bdate) throws ParseException { public static int daysBetween(Date smdate, Date bdate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
smdate = sdf.parse(sdf.format(smdate)); smdate = sdf.parse(sdf.format(smdate));
bdate = sdf.parse(sdf.format(bdate)); bdate = sdf.parse(sdf.format(bdate));
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.setTime(smdate); cal.setTime(smdate);
long time1 = cal.getTimeInMillis(); long time1 = cal.getTimeInMillis();
cal.setTime(bdate); cal.setTime(bdate);
long time2 = cal.getTimeInMillis(); long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24); long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days)); return Integer.parseInt(String.valueOf(between_days));
} }
/** /**
* 字符串的日期格式的计算 * 字符串的日期格式的计算
*/ */
public static int daysBetween(String smdate, String bdate) throws Exception { public static int daysBetween(String smdate, String bdate) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate)); cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis(); long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate)); cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis(); long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24); long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days)); return Integer.parseInt(String.valueOf(between_days));
} }
public static String getDateAfterDays(String startdate, int days) throws ParseException { public static String getDateAfterDays(String startdate, int days) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
Date dateStart = sdf.parse(startdate); Date dateStart = sdf.parse(startdate);
cal.setTime(dateStart); cal.setTime(dateStart);
long ms1 = dateStart.getTime(); long ms1 = dateStart.getTime();
long longday = days; long longday = days;
long ms2 = (long) (longday * 24 * 60 * 60 * 1000); long ms2 = (long) (longday * 24 * 60 * 60 * 1000);
long ms = ms1 + ms2; long ms = ms1 + ms2;
GregorianCalendar gd = new GregorianCalendar(); GregorianCalendar gd = new GregorianCalendar();
gd.setTimeInMillis(ms); gd.setTimeInMillis(ms);
Date dateEnd = gd.getTime(); Date dateEnd = gd.getTime();
String string = sdf.format(dateEnd); String string = sdf.format(dateEnd);
return string; return string;
} }
public static String getDateBeforeDays(String startdate, int days) throws ParseException { public static String getDateBeforeDays(String startdate, int days) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
Date dateStart = sdf.parse(startdate); Date dateStart = sdf.parse(startdate);
cal.setTime(dateStart); cal.setTime(dateStart);
long ms1 = dateStart.getTime(); long ms1 = dateStart.getTime();
long longday = days; long longday = days;
long ms2 = (long) (longday * 24 * 60 * 60 * 1000); long ms2 = (long) (longday * 24 * 60 * 60 * 1000);
long ms = ms1 - ms2; long ms = ms1 - ms2;
GregorianCalendar gd = new GregorianCalendar(); GregorianCalendar gd = new GregorianCalendar();
gd.setTimeInMillis(ms); gd.setTimeInMillis(ms);
Date dateEnd = gd.getTime(); Date dateEnd = gd.getTime();
String string = sdf.format(dateEnd); String string = sdf.format(dateEnd);
return string; return string;
} }
public static String getyyyyMMdd(int year, int month, int day) { public static String getyyyyMMdd(int year, int month, int day) {
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd");
GregorianCalendar gd = new GregorianCalendar(year, month, day); GregorianCalendar gd = new GregorianCalendar(year, month, day);
Date date = gd.getTime(); Date date = gd.getTime();
String string = mFormat.format(date); String string = mFormat.format(date);
return string; return string;
} }
public static String getyyyyMMdd(Date date) { public static String getyyyyMMdd(Date date) {
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd");
String string = mFormat.format(date); String string = mFormat.format(date);
return string; return string;
} }
/** /**
* 判断是否晚于今天 * 判断是否晚于今天
* *
* @param year * @param year
* @param month * @param month
* @param day * @param day
* @return * @return
*/ */
public static boolean isAfterToday(int year, int month, int day) { public static boolean isAfterToday(int year, int month, int day) {
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
int cY = c.get(Calendar.YEAR); int cY = c.get(Calendar.YEAR);
if (cY >= year) { if (cY >= year) {
return false; return false;
} }
if (cY < year) { if (cY < year) {
return true; return true;
} }
int cM = c.get(Calendar.MONTH) + 1; int cM = c.get(Calendar.MONTH) + 1;
if (cM >= month) { if (cM >= month) {
return false; return false;
} }
if (cM < month) { if (cM < month) {
return true; return true;
} }
int cD = c.get(Calendar.DAY_OF_MONTH); int cD = c.get(Calendar.DAY_OF_MONTH);
if (cD >= day) { if (cD >= day) {
return false; return false;
} }
if (cD < day) { if (cD < day) {
return true; return true;
} }
return false; return false;
} }
/** /**
* 判断日期是不是在日期区间里(包含) * 判断日期是不是在日期区间里(包含)
* *
* @param date 要判断的日期 yyyy-MM-dd * @param date 要判断的日期 yyyy-MM-dd
* @param minDate 最小日期 yyyy-MM-dd * @param minDate 最小日期 yyyy-MM-dd
* @param maxDate 最大日期 yyyy-MM-dd * @param maxDate 最大日期 yyyy-MM-dd
* @return * @return
*/ */
public static boolean isDateIn(String date, String minDate, String maxDate) { public static boolean isDateIn(String date, String minDate, String maxDate) {
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd");
try { try {
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
c.setTime(mFormat.parse(date)); c.setTime(mFormat.parse(date));
Calendar minC = Calendar.getInstance(); Calendar minC = Calendar.getInstance();
minC.setTime(mFormat.parse(minDate)); minC.setTime(mFormat.parse(minDate));
Calendar maxC = Calendar.getInstance(); Calendar maxC = Calendar.getInstance();
maxC.setTime(mFormat.parse(maxDate)); maxC.setTime(mFormat.parse(maxDate));
if (c.getTimeInMillis() >= minC.getTimeInMillis() if (c.getTimeInMillis() >= minC.getTimeInMillis()
&& c.getTimeInMillis() <= maxC.getTimeInMillis()) { && c.getTimeInMillis() <= maxC.getTimeInMillis()) {
return true; return true;
} }
} catch (ParseException e) { } catch (ParseException e) {
} }
return false; return false;
} }
/** /**
* 返回yyyy年M月d日的日期(如:2013年9月5日、2013年11月12日) * 返回yyyy年M月d日的日期(如:2013年9月5日、2013年11月12日)
* *
* @param date * @param date
* @return * @return
*/ */
public static String getChineseDate(Date date, String dfmt) { public static String getChineseDate(Date date, String dfmt) {
String fmt = "yyyy年M月d日"; String fmt = "yyyy年M月d日";
SimpleDateFormat f = new SimpleDateFormat(dfmt == null ? fmt : dfmt); SimpleDateFormat f = new SimpleDateFormat(dfmt == null ? fmt : dfmt);
return f.format(date); return f.format(date);
} }
/** /**
* clone Calendar * clone Calendar
* *
* @param c * @param c
* @return * @return
*/ */
public static Calendar cloneCalendar(Calendar c) { public static Calendar cloneCalendar(Calendar c) {
Calendar clone = (Calendar) c.clone(); Calendar clone = (Calendar) c.clone();
clone.setTime(c.getTime()); clone.setTime(c.getTime());
return clone; return clone;
} }
/** /**
* 获取两个时间之间相差的月份数组 * 获取两个时间之间相差的月份数组
* *
* @param start 开始时间 * @param start 开始时间
* @param end 结束时间 * @param end 结束时间
* @return 两个时间的相差的月份数 * @return 两个时间的相差的月份数
*/ */
public static int getMonthsOffset(Date start, Date end) { public static int getMonthsOffset(Date start, Date end) {
int offset = 0; int offset = 0;
Calendar c1 = Calendar.getInstance(); Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance();
c1.setTime(start); c1.setTime(start);
c2.setTime(end); c2.setTime(end);
while (c1.compareTo(c2) < 0) { while (c1.compareTo(c2) < 0) {
offset++; offset++;
c1.add(Calendar.MONTH, 1);// 开始日期加一个月直到等于结束日期为止 c1.add(Calendar.MONTH, 1);// 开始日期加一个月直到等于结束日期为止
} }
return offset; return offset;
} }
/** /**
* @param year * @param year
* @param month * @param month
* @param day * @param day
* @return 根据日期获取完整的农历信息 输出格式getAllLunar("2010", "7", "18") 农历 【虎】贰零壹零 年 * @return 根据日期获取完整的农历信息 输出格式getAllLunar("2010", "7", "18") 农历 【虎】贰零壹零 年
* 六月小廿三 庚寅年癸未月乙酉日 * 六月小廿三 庚寅年癸未月乙酉日
*/ */
public static String getAllLunar(String year, String month, String day) public static String getAllLunar(String year, String month, String day)
throws Exception { throws Exception {
Date sDObj; Date sDObj;
String s; String s;
int SY, SM, SD; int SY, SM, SD;
int sy; int sy;
SY = Integer.parseInt(year); SY = Integer.parseInt(year);
SM = Integer.parseInt(month); SM = Integer.parseInt(month);
SD = Integer.parseInt(day); SD = Integer.parseInt(day);
sy = (SY - 4) % 12; sy = (SY - 4) % 12;
Calendar cl = Calendar.getInstance(); Calendar cl = Calendar.getInstance();
cl.set(SY, SM - 1, SD); cl.set(SY, SM - 1, SD);
sDObj = cl.getTime(); sDObj = cl.getTime();
// 日期 // 日期
Sun2Lunar(sDObj); // 农历 Sun2Lunar(sDObj); // 农历
s = "农历 " + "【" + Animals[sy] + "】" + cYear(getYear()) + "年" + " "; s = "农历 " + "【" + Animals[sy] + "】" + cYear(getYear()) + "年" + " ";
s += (getIsLeap() ? "闰" : "") + monthNong[getMonth()] + "月" s += (getIsLeap() ? "闰" : "") + monthNong[getMonth()] + "月"
+ (monthDays(getYear(), getMonth()) == 29 ? "小" : "大"); + (monthDays(getYear(), getMonth()) == 29 ? "小" : "大");
s += cDay(getDay()) + " "; s += cDay(getDay()) + " ";
s += cyclical(getYearCyl()) + "年" + cyclical(getMonCyl()) + "月" s += cyclical(getYearCyl()) + "年" + cyclical(getMonCyl()) + "月"
+ cyclical(getDayCyl()) + "日"; + cyclical(getDayCyl()) + "日";
return s; return s;
} }
/** /**
* @param year * @param year
* @param month * @param month
* @param day * @param day
* @return 根据阳历日期获取阴历年份 * @return 根据阳历日期获取阴历年份
* @throws Exception * @throws Exception
*/ */
public static String getLauarYear(String year, String month, String day) throws Exception { public static String getLauarYear(String year, String month, String day) throws Exception {
getAllLunar(year, month, day); getAllLunar(year, month, day);
return cYear(getYear()); return cYear(getYear());
} }
/** /**
* @param year * @param year
* @param month * @param month
* @param day * @param day
* @return 根据阳历日期获取阴历月份 * @return 根据阳历日期获取阴历月份
* @throws Exception * @throws Exception
*/ */
public static String getLauarMonth(String year, String month, String day) throws Exception { public static String getLauarMonth(String year, String month, String day) throws Exception {
getAllLunar(year, month, day); getAllLunar(year, month, day);
return monthNong[getMonth()]; return monthNong[getMonth()];
} }
/** /**
* @param year * @param year
* @param month * @param month
* @param day * @param day
* @return 根据阳历日期获取阴历日 * @return 根据阳历日期获取阴历日
* @throws Exception * @throws Exception
*/ */
public static String getLauarDay(String year, String month, String day) throws Exception { public static String getLauarDay(String year, String month, String day) throws Exception {
getAllLunar(year, month, day); getAllLunar(year, month, day);
return cDay(getDay()); return cDay(getDay());
} }
/** /**
* @param year * @param year
* @param month * @param month
* @param day * @param day
* @return 根据阳历日期获取属相 * @return 根据阳历日期获取属相
*/ */
public static String getZodiac(String year, String month, String day) public static String getZodiac(String year, String month, String day)
throws Exception { throws Exception {
getAllLunar(year, month, day); getAllLunar(year, month, day);
int SY, sy; int SY, sy;
SY = getYear(); SY = getYear();
sy = (SY - 4) % 12; sy = (SY - 4) % 12;
return Animals[sy]; return Animals[sy];
} }
/** /**
* @param sunyear * @param sunyear
* @param sunmonth * @param sunmonth
* @param sunday * @param sunday
* @return 根据阳历日期获取星座 * @return 根据阳历日期获取星座
*/ */
public static String getConstellation(String sunyear, String sunmonth, String sunday) public static String getConstellation(String sunyear, String sunmonth, String sunday)
throws Exception { throws Exception {
int m = Integer.parseInt(sunmonth); int m = Integer.parseInt(sunmonth);
int d = Integer.parseInt(sunday); int d = Integer.parseInt(sunday);
if (d < constellationEdgeDay[m]) { if (d < constellationEdgeDay[m]) {
m = m - 1; m = m - 1;
} }
if (m >= 0) { if (m >= 0) {
return constellationArr[m]; return constellationArr[m];
} }
return constellationArr[11]; return constellationArr[11];
} }
private static int monCyl, dayCyl, yearCyl; private static int monCyl, dayCyl, yearCyl;
private static int year, month, day; private static int year, month, day;
private static boolean isLeap; private static boolean isLeap;
public static final String[] constellationArr = { public static final String[] constellationArr = {
"魔羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座",
"魔羯座" "魔羯座"
}; };
public static final int[] constellationEdgeDay = { public static final int[] constellationEdgeDay = {
30, 20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22 30, 20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22
}; };
private static int[] lunarInfo = { private static int[] lunarInfo = {
0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x04bd8, 0x04ae0, 0x0a570, 0x054d5,
0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, 0x04ae0, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, 0x04ae0,
0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2,
0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40,
0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566, 0x0d4a0, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566, 0x0d4a0,
0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7,
0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0,
0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550, 0x15355, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550, 0x15355,
0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0,
0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263,
0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0,
0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6, 0x095b0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6, 0x095b0,
0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46,
0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50,
0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 0x0d954, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 0x0d954,
0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0,
0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0,
0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0x0ad50, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0x0ad50,
0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6,
0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0,
0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0
}; };
// private static int[] solarMonth = { // private static int[] solarMonth = {
// 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, // 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
// //
// 30, 31 // 30, 31
// }; // };
private static String[] Gan = { private static String[] Gan = {
"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛",
"壬", "癸" "壬", "癸"
}; };
private static String[] Zhi = { private static String[] Zhi = {
"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "子", "丑", "寅", "卯", "辰", "巳", "午", "未",
"申", "酉", "戌", "亥" "申", "酉", "戌", "亥"
}; };
private static String[] Animals = { private static String[] Animals = {
"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊",
"猴", "鸡", "狗", "猪" "猴", "鸡", "狗", "猪"
}; };
// private static int[] sTermInfo = { // private static int[] sTermInfo = {
// 0, 21208, 42467, 63836, 85337, 107014, // 0, 21208, 42467, 63836, 85337, 107014,
// 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, // 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989,
// 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, // 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224,
// 483532, 504758 // 483532, 504758
// }; // };
private static String[] nStr1 = { private static String[] nStr1 = {
"日", "一", "二", "三", "四", "五", "六", "七", "日", "一", "二", "三", "四", "五", "六", "七",
"八", "九", "十" "八", "九", "十"
}; };
private static String[] nStr2 = { private static String[] nStr2 = {
"初", "十", "廿", "卅", " " "初", "十", "廿", "卅", " "
}; };
private static String[] monthNong = { private static String[] monthNong = {
"正", "正", "二", "三", "四", "五", "六", "正", "正", "二", "三", "四", "五", "六",
"七", "八", "九", "十", "十一", "十二" "七", "八", "九", "十", "十一", "十二"
}; };
private static String[] yearName = { private static String[] yearName = {
"零", "壹", "贰", "叁", "肆", "伍", "陆", "零", "壹", "贰", "叁", "肆", "伍", "陆",
"柒", "捌", "玖" "柒", "捌", "玖"
}; };
// 传回农历 y年的总天数 // 传回农历 y年的总天数
private static int lYearDays(int y) { private static int lYearDays(int y) {
int i; int i;
int sum = 348; // 29*12 int sum = 348; // 29*12
for (i = 0x8000; i > 0x8; i >>= 1) { for (i = 0x8000; i > 0x8; i >>= 1) {
sum += (lunarInfo[y - 1900] & i) == 0 ? 0 : 1; // 大月+1天 sum += (lunarInfo[y - 1900] & i) == 0 ? 0 : 1; // 大月+1天
} }
return (sum + leapDays(y)); // +闰月的天数 return (sum + leapDays(y)); // +闰月的天数
} }
// 传回农历 y年闰月的天数 // 传回农历 y年闰月的天数
private static int leapDays(int y) { private static int leapDays(int y) {
if (leapMonth(y) != 0) if (leapMonth(y) != 0)
return ((lunarInfo[y - 1900] & 0x10000) == 0 ? 29 : 30); return ((lunarInfo[y - 1900] & 0x10000) == 0 ? 29 : 30);
else else
return (0); return (0);
} }
// 传回农历 y年闰哪个月 1-12 , 没闰传回 0 // 传回农历 y年闰哪个月 1-12 , 没闰传回 0
private static int leapMonth(int y) { private static int leapMonth(int y) {
return (lunarInfo[y - 1900] & 0xf); return (lunarInfo[y - 1900] & 0xf);
} }
// 传回农历 y年m月的总天数 // 传回农历 y年m月的总天数
private static int monthDays(int y, int m) { private static int monthDays(int y, int m) {
return ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0 ? 29 : 30); return ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0 ? 29 : 30);
} }
private static void Sun2Lunar(Date objDate) { private static void Sun2Lunar(Date objDate) {
int i, leap = 0, temp = 0; int i, leap = 0, temp = 0;
Calendar cl = Calendar.getInstance(); Calendar cl = Calendar.getInstance();
cl.set(1900, 0, 31); cl.set(1900, 0, 31);
Date baseDate = cl.getTime(); Date baseDate = cl.getTime();
int offset = (int) ((objDate.getTime() - baseDate.getTime()) / 86400000); // 天数(86400000=24*60*60*1000) int offset = (int) ((objDate.getTime() - baseDate.getTime()) / 86400000); // 天数(86400000=24*60*60*1000)
dayCyl = offset + 40; // 1899-12-21是农历1899年腊月甲子日 dayCyl = offset + 40; // 1899-12-21是农历1899年腊月甲子日
monCyl = 14; // 1898-10-01是农历甲子月 monCyl = 14; // 1898-10-01是农历甲子月
// 得到年数 // 得到年数
for (i = 1900; i < 2050 && offset > 0; i++) { for (i = 1900; i < 2050 && offset > 0; i++) {
temp = lYearDays(i); // 农历每年天数 temp = lYearDays(i); // 农历每年天数
offset -= temp; offset -= temp;
monCyl += 12; monCyl += 12;
} }
if (offset < 0) { if (offset < 0) {
offset += temp; offset += temp;
i--; i--;
monCyl -= 12; monCyl -= 12;
} }
year = i; // 农历年份 year = i; // 农历年份
yearCyl = i - 1864; // 1864年是甲子年 yearCyl = i - 1864; // 1864年是甲子年
leap = leapMonth(i); // 闰哪个月 leap = leapMonth(i); // 闰哪个月
isLeap = false; isLeap = false;
for (i = 1; i < 13 && offset > 0; i++) { for (i = 1; i < 13 && offset > 0; i++) {
// 闰月 // 闰月
if (leap > 0 && i == (leap + 1) && isLeap == false) { if (leap > 0 && i == (leap + 1) && isLeap == false) {
--i; --i;
isLeap = true; isLeap = true;
temp = leapDays(year); temp = leapDays(year);
} else { } else {
temp = monthDays(year, i); temp = monthDays(year, i);
} }
// 解除闰月 // 解除闰月
if (isLeap == true && i == (leap + 1)) if (isLeap == true && i == (leap + 1))
isLeap = false; isLeap = false;
offset -= temp; offset -= temp;
if (isLeap == false) if (isLeap == false)
monCyl++; monCyl++;
} }
if (offset == 0 && leap > 0 && i == leap + 1) if (offset == 0 && leap > 0 && i == leap + 1)
if (isLeap) { if (isLeap) {
isLeap = false; isLeap = false;
} else { } else {
isLeap = true; isLeap = true;
--i; --i;
--monCyl; --monCyl;
} }
if (offset < 0) { if (offset < 0) {
offset += temp; offset += temp;
--i; --i;
--monCyl; --monCyl;
} }
month = i; // 农历月份 month = i; // 农历月份
day = offset + 1; // 农历天份 day = offset + 1; // 农历天份
} }
// 传入 offset 传回干支, 0=甲子 // 传入 offset 传回干支, 0=甲子
private static String cyclical(int num) { private static String cyclical(int num) {
return (Gan[num % 10] + Zhi[num % 12]); return (Gan[num % 10] + Zhi[num % 12]);
} }
// 中文日期 // 中文日期
private static String cDay(int d) { private static String cDay(int d) {
String s; String s;
switch (d) { switch (d) {
case 10: case 10:
s = "初十"; s = "初十";
break; break;
case 20: case 20:
s = "二十"; s = "二十";
break; break;
case 30: case 30:
s = "三十"; s = "三十";
break; break;
default: default:
s = nStr2[(int) (d / 10)];// 取商 s = nStr2[(int) (d / 10)];// 取商
s += nStr1[d % 10];// 取余 s += nStr1[d % 10];// 取余
} }
return (s); return (s);
} }
private static String cYear(int y) { private static String cYear(int y) {
String s = " "; String s = " ";
int d; int d;
while (y > 0) { while (y > 0) {
d = y % 10; d = y % 10;
y = (y - d) / 10; y = (y - d) / 10;
s = yearName[d] + s; s = yearName[d] + s;
} }
return (s); return (s);
} }
private static int getYear() { private static int getYear() {
return (year); return (year);
} }
private static int getMonth() { private static int getMonth() {
return (month); return (month);
} }
private static int getDay() { private static int getDay() {
return (day); return (day);
} }
private static int getMonCyl() { private static int getMonCyl() {
return (monCyl); return (monCyl);
} }
private static int getYearCyl() { private static int getYearCyl() {
return (yearCyl); return (yearCyl);
} }
private static int getDayCyl() { private static int getDayCyl() {
return (dayCyl); return (dayCyl);
} }
private static boolean getIsLeap() { private static boolean getIsLeap() {
return (isLeap); return (isLeap);
} }
public static String getyyyyMMddHHmm(long ms) { public static String getyyyyMMddHHmm(long ms) {
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(ms); Date date = new Date(ms);
String string = mFormat.format(date); String string = mFormat.format(date);
return string; return string;
} }
public static String getTime(String time) { public static String getTime(String time) {
try { try {
long longtime = Long.parseLong(time); long longtime = Long.parseLong(time);
return getyyyyMMddHHmm(longtime); return getyyyyMMddHHmm(longtime);
} catch (Exception e) { } catch (Exception e) {
return ""; return "";
} }
} }
public static String getNoticeTime(String time) { public static String getNoticeTime(String time) {
time = time.substring(0, time.length() - 3); time = time.substring(0, time.length() - 3);
return getTime(time); return getTime(time);
} }
public static long getLongFromyyyyMMddHHmm(String date) { public static long getLongFromyyyyMMddHHmm(String date) {
SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date parse = null; Date parse = null;
try { try {
parse = mFormat.parse(date); parse = mFormat.parse(date);
} catch (ParseException e) { } catch (ParseException e) {
} }
if (parse != null) { if (parse != null) {
return parse.getTime(); return parse.getTime();
} else { } else {
return -1; return -1;
} }
} }
// @SuppressLint("SimpleDateFormat") // @SuppressLint("SimpleDateFormat")
// public static long parseDate(String date, String pattern) throws // public static long parseDate(String date, String pattern) throws
// ParseException { // ParseException {
// SimpleDateFormat f = new SimpleDateFormat(pattern); // SimpleDateFormat f = new SimpleDateFormat(pattern);
// return f.parse(date).getTime(); // return f.parse(date).getTime();
// } // }
@SuppressLint("SimpleDateFormat") @SuppressLint("SimpleDateFormat")
public static String formatDate(long date, String pattern) { public static String formatDate(long date, String pattern) {
SimpleDateFormat f = new SimpleDateFormat(pattern); SimpleDateFormat f = new SimpleDateFormat(pattern);
return f.format(new Date(date)); return f.format(new Date(date));
} }
public static String changeFormat(String date, String srcPattern, String destPattern) public static String changeFormat(String date, String srcPattern, String destPattern)
throws ParseException { throws ParseException {
long l = parseDate(date, srcPattern); long l = parseDate(date, srcPattern);
return formatDate(l, destPattern); return formatDate(l, destPattern);
} }
/** /**
* for example : yyyy-MM-dd HH:mm:ss * for example : yyyy-MM-dd HH:mm:ss
* *
* @param pattern * @param pattern
* @return * @return
*/ */
@SuppressLint("SimpleDateFormat") @SuppressLint("SimpleDateFormat")
public static String getDateString(String pattern) { public static String getDateString(String pattern) {
SimpleDateFormat ff = new SimpleDateFormat(pattern); SimpleDateFormat ff = new SimpleDateFormat(pattern);
return ff.format(new Date()); return ff.format(new Date());
} }
public static int getWeekday(String time, String format) { public static int getWeekday(String time, String format) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format); SimpleDateFormat dateFormat = new SimpleDateFormat(format);
Date date; Date date;
try { try {
date = dateFormat.parse(time); date = dateFormat.parse(time);
} catch (ParseException e) { } catch (ParseException e) {
date = new Date(); date = new Date();
} }
Calendar cd = Calendar.getInstance(); Calendar cd = Calendar.getInstance();
cd.setTime(date); cd.setTime(date);
cd.setFirstDayOfWeek(Calendar.MONDAY); cd.setFirstDayOfWeek(Calendar.MONDAY);
int result = cd.get(Calendar.DAY_OF_WEEK) - 1; int result = cd.get(Calendar.DAY_OF_WEEK) - 1;
if (result == 0) { if (result == 0) {
result = 7; result = 7;
} }
return result; return result;
} }
public static String getFormateDate(Date date, String format) { public static String getFormateDate(Date date, String format) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format); SimpleDateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.format(date); return dateFormat.format(date);
} }
/* xsm, add, 20150414 */ /* xsm, add, 20150414 */
public static long parseDate(String time, String format) { public static long parseDate(String time, String format) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format); SimpleDateFormat dateFormat = new SimpleDateFormat(format);
Date date; Date date;
try { try {
date = dateFormat.parse(time); date = dateFormat.parse(time);
} catch (ParseException e) { } catch (ParseException e) {
date = new Date(); date = new Date();
} }
return date.getTime(); return date.getTime();
} }
public static long calcDifference(Date startDate) { public static long calcDifference(Date startDate) {
long result = 0; long result = 0;
if (startDate != null) { if (startDate != null) {
Date endDate = new Date(); Date endDate = new Date();
//milliseconds //milliseconds
long different = startDate.getTime() - endDate.getTime(); long different = startDate.getTime() - endDate.getTime();
long secondsInMilli = 1000; long secondsInMilli = 1000;
long elapsedSeconds = different / secondsInMilli; long elapsedSeconds = different / secondsInMilli;
result = elapsedSeconds; result = elapsedSeconds;
} }
return result; return result;
} }
public static String getTime(Date date) {//可根据需要自行截取数据显示 public static String getTime(Date date) {//可根据需要自行截取数据显示
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日"); SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
return format.format(date); return format.format(date);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment