package cn.hnedu.util;
import java.io.*;
import java.text.*;
import java.util.*;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
public DateUtils() {
}
public final static Calendar myc = Calendar.getInstance();
public static final int SECOND = 1000;
public static final int MINUTE = SECOND * 60;
public static final int HOUR = MINUTE * 60;
public static final int DAY = HOUR * 24;
public static final int WEEK = DAY * 7;
public static final int YEAR = DAY * 365; // or 366 ???
public static final long GMT_VIETNAM_TIME_OFFSET = HOUR * 8;
private static long SERVER_TIME_OFFSET = HOUR * 0;
private static DateFormat ddMMyyyyFormat = new SimpleDateFormat("dd/MM/yyyy");
private static DateFormat yyyymmddhhmmssFormat = new SimpleDateFormat(
"yyyy年MM月dd日 kk:mm:ss");
private static DateFormat mmddhhmmFormat = new SimpleDateFormat(
"MM月dd日 kk:mm");
private static DateFormat yyyymmddhhmmFormat = new SimpleDateFormat(
"yyyy年MM月dd日 kk:mm");
private static DateFormat dateFormat = SimpleDateFormat.getDateInstance(
SimpleDateFormat.DEFAULT);
private static DateFormat datetimeFormat = SimpleDateFormat.
getDateTimeInstance(SimpleDateFormat.DEFAULT, SimpleDateFormat.DEFAULT);
/**
* @return 返回当前时间,返回格式:2003-4-25 14:50:7
*/
public static String getTime() {
return getTime("yyyy-MM-dd HH:mm:ss");
}
public static Date getDate(String parrten) {
if (!StringUtils.hasText(parrten)) {
parrten = "yyyyMMddHH";
}
String starttime = DateUtils.getTime(parrten);
return DateUtils.getStrToDate(starttime, parrten);
}
public static Date getDate(){
return getDate("yyyy-MM-dd HH:mm:ss");
}
/**
* 获得当前时间
* @param parrten 输出的时间格式
* @return 返回时间
*/
public static String getTime(String parrten) {
String timestr;
if (parrten == null || parrten.equals("")) {
parrten = "yyyyMMddHHmmss";
}
java.text.SimpleDateFormat sdf = new SimpleDateFormat(parrten);
java.util.Date cday = new Date();
timestr = sdf.format(cday);
return timestr;
}
/**
* 时间格式转换
* @param cday
* @param parrten
* @return
*/
public static String getTime(Date cday, String parrten) {
String timestr;
if (parrten == null || parrten.equals("")) {
parrten = "yyyyMMddHHmmss";
}
java.text.SimpleDateFormat sdf = new SimpleDateFormat(parrten);
timestr = sdf.format(cday);
return timestr;
}
/**
* 得到昨天的时间
* @return
*/
public static String getYestday() {
String timestr;
java.util.Calendar cc = myc;
cc.roll(cc.DAY_OF_YEAR, -1);
java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
timestr = sdf.format(cc.getTime());
return timestr;
}
//将一个时间字符串转换为date型
public static Date getStrToDate(String time, String parrten) {
SimpleDateFormat formatter = new SimpleDateFormat(parrten);
ParsePosition pos = new ParsePosition(0);
Date dt1 = formatter.parse(time, pos);
return dt1;
}
/**
* 将时间转换为xxxx年xx月xx日格式
* @param t1 原时间
* @param parrten 原时间格式
* @return
*/
public static String getTime(String t1, String parrten) {
SimpleDateFormat formatter = new SimpleDateFormat(parrten);
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy年MM月dd日");
ParsePosition pos = new ParsePosition(0);
Date dt1 = formatter.parse(t1, pos);
return formatter2.format(dt1);
}
/**
* 将时间转换为parrten2格式
* @param t1
* @param parrten
* @param parrten2
* @return
*/
public static String getTime(String t1, String parrten, String parrten2) {
SimpleDateFormat formatter = new SimpleDateFormat(parrten);
SimpleDateFormat formatter2 = new SimpleDateFormat(parrten2);
ParsePosition pos = new ParsePosition(0);
Date dt1 = formatter.parse(t1, pos);
return formatter2.format(dt1);
}
/**
* 比较两个字符串时间的大小
* @param t1 时间1
* @param t2 时间2
* @param parrten 时间格式 :yyyy-MM-dd
* @return 返回long =0相等,>0 t1>t2,<0 t1<t2
*/
public static long compareStringTime(String t1, String t2, String parrten) {
SimpleDateFormat formatter = new SimpleDateFormat(parrten);
ParsePosition pos = new ParsePosition(0);
ParsePosition pos1 = new ParsePosition(0);
Date dt1 = formatter.parse(t1, pos);
Date dt2 = formatter.parse(t2, pos1);
long l = dt1.getTime() - dt2.getTime();
return l;
}
//相差的天数
public static int compareTime(String time1, String time2) {
long l = compareStringTime(time1, time2, "yyyy-MM-dd");
long days = l / 1000 / 60 / 60 / 24;
return (int) days;
}
public static synchronized String getDateDDMMYYYY(Date date) {
return ddMMyyyyFormat.format(date);
}
public static synchronized String getDateindex(Date date) {
return yyyymmddhhmmssFormat.format(date);
}
public static synchronized String getDateSearch(Date date) {
return mmddhhmmFormat.format(date);
}
public static synchronized String getDateNoSencond(Date date) {
return yyyymmddhhmmFormat.format(date);
}
public static synchronized String formatDate(Date date) {
return dateFormat.format(date);
}
public static synchronized String formatDateTime(Date date) {
return datetimeFormat.format(date);
}
public static Timestamp getCurrentGMTTimestamp() {
return new Timestamp(System.currentTimeMillis() - SERVER_TIME_OFFSET);
}
public static void updateCurrentGMTTimestamp(Timestamp timeToUpdate) {
timeToUpdate.setTime(System.currentTimeMillis() - SERVER_TIME_OFFSET);
}
public static Date getVietnamDateFromGMTDate(Date date) {
return new Date(date.getTime() + GMT_VIETNAM_TIME_OFFSET);
}
public static Date convertGMTDate(Date gmtDate, int hourOffset) {
return new Date(gmtDate.getTime() + hourOffset * HOUR);
}
public static Timestamp convertGMTTimestamp(Timestamp gmtTimestamp,
int hourOffset) {
return new Timestamp(gmtTimestamp.getTime() + hourOffset * HOUR);
}
public static Timestamp getCurrentGMTTimestampExpiredYear(int offsetYear) {
//return new Timestamp(System.currentTimeMillis() + offsetYear*(365*24*60*60*1000));
Calendar now = Calendar.getInstance();
now.add(Calendar.YEAR, offsetYear);
return new Timestamp(now.getTime().getTime());
}
public static Timestamp getCurrentGMTTimestampExpiredMonth(int offsetMonth) {
Calendar now = Calendar.getInstance();
now.add(Calendar.MONTH, offsetMonth);
return new Timestamp(now.getTime().getTime());
}
/**
* 在当前的日期前加上N天
* @param day int 天数
* @return Date
*/
public static Date addDay(int day) {
java.util.Date date1 = new Date();
long Time = (date1.getTime() / 1000) + 60 * 60 * 24 * day;
date1.setTime(Time * 1000);
return date1;
}
/**
* 计算两个时间之间的天数
* @param d1 Date
* @param d2 Date
* @return long
*/
public static long getDaysFromTwoDate(Date d1, Date d2) {
long day = (d1.getTime() - d2.getTime()) / (24 * 60 * 60 * 1000);
return day;
}
public static Date getLastSunday() {
String sdate = getTime("yyyy-MM-dd");
int syear = Integer.parseInt(sdate.substring(0, 4));
int smonth = Integer.parseInt(sdate.substring(5, 7));
int sday = Integer.parseInt(sdate.substring(8, 10));
GregorianCalendar cal = new GregorianCalendar();
cal.set(syear, smonth, sday);
int week = cal.get(java.util.Calendar.DAY_OF_WEEK); //1为星期天
return addDay( - (week + 6));
}
public static Date getLastSaturday() {
String sdate = getTime("yyyy-MM-dd");
int syear = Integer.parseInt(sdate.substring(0, 4));
int smonth = Integer.parseInt(sdate.substring(5, 7));
int sday = Integer.parseInt(sdate.substring(8, 10));
GregorianCalendar cal = new GregorianCalendar();
cal.set(syear, smonth, sday);
int week = cal.get(java.util.Calendar.DAY_OF_WEEK); //1为星期天
return addDay( - (week));
}
public static String getlastMonth() {
String sdate = getTime("yyyy-MM-dd");
int syear = Integer.parseInt(sdate.substring(0, 4));
int smonth = Integer.parseInt(sdate.substring(5, 7));
smonth = smonth - 1;
if (smonth == 0) {
smonth = 12;
syear = syear - 1;
}
return syear + "-" + smonth + "-01";
}
public static String getThisMonthStart() {
String sdate = getTime("yyyy-MM-dd");
int syear = Integer.parseInt(sdate.substring(0, 4));
int smonth = Integer.parseInt(sdate.substring(5, 7));
return syear + "-" + smonth + "-01";
}
/**
* 当日属于一年的第几周
* @return int
*/
public static int getWeekOfYeah() {
Calendar calendar = Calendar.getInstance();
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
return week;
}
/**
* 得到一个星期的开始
* @return Date
*/
public static Date weekStart() {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
Date dateBegin = new Date();
dateBegin.setTime(date.getTime() - (long) (week) * 24 * 60 * 60 * 1000);
return dateBegin;
}
/**
* 得到一个星期的结束
* @return Date
*/
public static Date weekEnd() {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
Date dateEnd = new Date();
dateEnd.setTime(date.getTime() + (long) (6 - week) * 24 * 60 * 60 * 1000);
return dateEnd;
}
/**
* 得到某年第n个星期的星期天
* @param year int
* @param week int
* @return Date
*/
public static Date getDateByYearAndWeekstart(int year, int week) { //星期日。第一天
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.WEEK_OF_YEAR, week);
calendar.set(Calendar.DAY_OF_WEEK, 1);
return calendar.getTime();
}
public static String getQuarter(String month) {
if (!StringUtils.hasText(month) && !StringUtils.isNumeric(month)) {
month = "1";
}
int m = Integer.parseInt(month);
if (m <= 12 && m >= 10) {
return "4";
} else if (m <= 9 && m >= 7) {
return "3";
} else if (m <= 6 && m >= 4) {
return "2";
} else {
return "1";
}
}
public static String getQuarter(){
String month=getTime("M");
return getQuarter(month);
}
public static String getQuarterCn(){
String month=getTime("M");
return getQuarterCn(month);
}
public static String getQuarterCn(String month) {
if (!StringUtils.hasText(month) && !StringUtils.isNumeric(month)) {
month = "1";
}
int m = Integer.parseInt(month);
if (m <= 12 && m >= 10) {
return "四";
} else if (m <= 9 && m >= 7) {
return "三";
} else if (m <= 6 && m >= 4) {
return "二";
} else {
return "一";
}
}
/**
* 得到某年第n个星期的星期六
* @param year int
* @param week int
* @return Date
*/
public static Date getDateByYearAndWeekend(int year, int week) { //星期六。最后一天
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.WEEK_OF_YEAR, week);
calendar.set(Calendar.DAY_OF_WEEK, 0);
return calendar.getTime();
}
public static void main(String args[]) {
String stime="2007-08-24";
String etime="2007-08-28";
//System.out.println(DateUtils.compareTime(etime,stime));
//System.out.println(DateUtils.getTime("M"));
Float a=Float.valueOf("1");
Float b=Float.valueOf("1.9");
}