package org.eredlab.g4.ccl.util;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* @Description {日期多功能类}
* @author LIUBO
* @since 2011-8-3
*/
public class DateUtils {
public static String getCurrentDate() {
Timestamp ts = new Timestamp(System.currentTimeMillis());
return new SimpleDateFormat("yyyyMMdd").format(ts);
}
public static String getCurrentTime() {
Timestamp ts = new Timestamp(System.currentTimeMillis());
String currentTime = new SimpleDateFormat("HHmmss").format(ts);
return currentTime;
}
public static String getCurrentDate2() {
Timestamp ts = new Timestamp(System.currentTimeMillis());
return new SimpleDateFormat("yyyyMMdd").format(ts);
}
public static String getCurrentTime2() {
Timestamp ts = new Timestamp(System.currentTimeMillis());
String currentTime = new SimpleDateFormat("HH:mm:ss").format(ts);
return currentTime;
}
public static long getCurrentTimeLong() {
return Long.parseLong(getCurrentTime());
}
public static String getNextDate() {
Timestamp ts = new Timestamp(System.currentTimeMillis() + 86400000L);
return new SimpleDateFormat("yyyyMMdd").format(ts);
}
public static String getCurrentMonth() {
Timestamp ts = new Timestamp(System.currentTimeMillis());
return new SimpleDateFormat("MM").format(ts);
}
public static String getCurrentYear() {
Timestamp ts = new Timestamp(System.currentTimeMillis());
return new SimpleDateFormat("yyyy").format(ts);
}
public static int getCurrentYearInt() {
return Integer.parseInt(getCurrentYear());
}
public static int getNextYearInt() {
return (getCurrentYearInt() + 1);
}
public static String getCurrentDay() {
Timestamp ts = new Timestamp(System.currentTimeMillis());
return new SimpleDateFormat("dd").format(ts);
}
public static int getCurrentWeek() {
Calendar cd = Calendar.getInstance();
cd.setFirstDayOfWeek(2);
cd.setMinimalDaysInFirstWeek(7);
cd.setTime(new java.util.Date());
return cd.get(3);
}
public static int getMaxWeekOfYear() {
Calendar cd = Calendar.getInstance();
cd.setFirstDayOfWeek(2);
cd.setMinimalDaysInFirstWeek(7);
cd.setTime(new java.util.Date());
if ((cd.get(2) == 0) && (cd.get(5) < 7)) {
return cd.get(3);
}
cd.set(cd.get(1), 11, 31);
return cd.get(3);
}
/** @deprecated */
public static String getDifDate(int cl) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
Calendar c = Calendar.getInstance();
int d = c.get(5);
d += cl;
c.set(5, d);
return df.format(c.getTime());
}
/** @deprecated */
public static String getDifDate(String olddate, int cl) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
Calendar c = Calendar.getInstance();
c.set(1, Integer.parseInt(olddate.substring(0, 4)));
c.set(2, Integer.parseInt(olddate.substring(4, 6)) - 1);
c.set(5, Integer.parseInt(olddate.substring(6, 8)));
int d = c.get(5);
d += cl;
c.set(5, d);
return df.format(c.getTime());
}
public static int getMaxDayOfMonth() {
int dqnd = Integer.parseInt(getCurrentYear());
int dqyd = Integer.parseInt(getCurrentMonth());
return getMaxDayOfMonth(dqnd, dqyd);
}
public static int getMaxDayOfMonth(int year, int mon) {
int dqnd = year;
int dqyd = mon;
if (dqyd == 12) {
++dqnd;
dqyd = 1;
} else {
++dqyd;
}
java.sql.Date xydyt = java.sql.Date.valueOf(dqnd + "-" + dqyd + "-1");
Timestamp ts = new Timestamp(xydyt.getTime() - 86400000L);
String day = new SimpleDateFormat("dd").format(ts);
return Integer.parseInt(day);
}
public static String getString(java.sql.Date date) {
Timestamp ts = new Timestamp(date.getTime());
String day = new SimpleDateFormat("yyyyMMdd").format(ts);
return day;
}
public static long getDifday(String olddate, String newdate) {
String strri = olddate.substring(6, 8);
String stryd = olddate.substring(4, 6);
String strnd = olddate.substring(0, 4);
java.sql.Date dolddate = java.sql.Date.valueOf(strnd + "-" + stryd
+ "-" + strri);
strri = newdate.substring(6, 8);
stryd = newdate.substring(4, 6);
strnd = newdate.substring(0, 4);
java.sql.Date dnewdate = java.sql.Date.valueOf(strnd + "-" + stryd
+ "-" + strri);
long a = (dnewdate.getTime() - dolddate.getTime()) / 86400000L;
return a;
}
public static long getDifday(int year, int mon, int oldday, int newday) {
String strri = String.valueOf(oldday);
String stryd = String.valueOf(mon);
String strnd = String.valueOf(year);
java.sql.Date dolddate = java.sql.Date.valueOf(strnd + "-" + stryd
+ "-" + strri);
strri = String.valueOf(newday);
stryd = String.valueOf(mon);
strnd = String.valueOf(year);
java.sql.Date dnewdate = java.sql.Date.valueOf(strnd + "-" + stryd
+ "-" + strri);
long a = (dnewdate.getTime() - dolddate.getTime()) / 86400000L;
return a;
}
public static String[] getNext12Month(int year, int month) {
String[] ret = new String[12];
int nextYear = year;
int nextMonth = month;
for (int i = 0; i < 12; ++i) {
if (++nextMonth > 12) {
++nextYear;
nextMonth = 1;
}
ret[i] = new String(String.valueOf(nextYear) + "/"
+ String.valueOf(nextMonth));
}
return ret;
}
public static String[] getNext12Month() {
int year = Integer.parseInt(getCurrentYear());
int month = Integer.parseInt(getCurrentMonth());
return getNext12Month(year, month);
}
public static String parseTimeStr(long time) {
String ret = new String();
String temp = Long.toString(time);
if (temp.length() > 13)
ret = temp.substring(0, 4) + "年" + temp.substring(4, 6) + "月"
+ temp.substring(6, 8) + "日" + temp.substring(8, 10) + "时"
+ temp.substring(10, 12) + "分";
return ret;
}
/** @deprecated */
public static int date2int(String date) {
String temp = date.substring(0, date.indexOf("-"));
String a = date.substring(date.indexOf("-") + 1);
String b = a.substring(0, a.indexOf("-"));
temp = temp
+ ((b.length() >= 2) ? b : new StringBuffer("0").append(b)
.toString());
b = a.substring(a.indexOf("-") + 1);
temp = temp
+ ((b.length() >= 2) ? b : new StringBuffer("0").append(b)
.toString());
return Integer.parseInt(temp);
}
/** @deprecated */
public static String int2date(int inte) {
String a = String.valueOf(inte);
String temp = a.substring(0, 4);
temp = temp
+ "-"
+ ((a.substring(4, 5).equals("0")) ? a.substring(5, 6) : a
.substring(4, 6));
temp = temp
+ "-"
+ ((a.substring(6, 7).equals("0")) ? a.substring(7, 8) : a
.substring(6, 8));
return temp;
}
public static int getDayOfWeek(int year, int mon, int day) {
Calendar cal = new GregorianCalendar(year, mon - 1, day);
int dayOfWeek = cal.get(7);
if (dayOfWeek - 1 == 0) {
return 7;
}
return (dayOfWeek - 1);
}
public static java.util.Date ChangeDateFormat(String date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return new java.util.Date();
}
public static java.util.Date ChangeDateFormat(String date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return new java.util.Date();
}
public static String ChangeDateFormat(Timestamp time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
if (time == null) {
return sdf.format(new java.util.Date());
}
return sdf.format(time);
}
public static String ChangeDateFormat(Timestamp time, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
if (time == null) {
return sdf.format(new java.util.Date());
}
return sdf.format(time);
}
public static String ChangeDateFormat(java.util.Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
if (date == null) {
return sdf.format(new java.util.Date());
}
return sdf.format(date);
}
public static String ChangeDateFormat(java.util.Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
if (date == null) {
return sdf.format(new java.util.Date());
}
return sdf.format(date);
}
}