日期之间的转化关系:
1,获取当前系统时间
Date date = new Date();
2,Date转为DateTime
DateTime dateTime = new DateTime(date.getTime());
3,DateTime转为Date
Date date = dateTime.toDate();
4,获取日期格式,
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
5,Date转换为String类型格式
String dateStr = df.format(new Date());
工具类
public static String getCurrentTime() {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
returnStr = f.format(date);
return returnStr;
}
public static BigDecimal getCurrentTimeAsNumber() {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
returnStr = f.format(date);
return new BigDecimal(returnStr);
}
public static String getCurrentTime(String format) {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat(format);
Date date = new Date();
returnStr = f.format(date);
return returnStr;
}
public static String getCurDate() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
String strDate = simpledateformat.format(calendar.getTime());
return strDate;
}
public static String Date2String(Date date, String formatString) {
if (G4Utils.isEmpty(date)) {
return null;
}
SimpleDateFormat simpledateformat = new SimpleDateFormat(formatString);
String strDate = simpledateformat.format(date);
return strDate;
}
public static String getCurDate(String format) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
String strDate = simpledateformat.format(calendar.getTime());
return strDate;
}
public static Timestamp getCurrentTimestamp() {
Object obj = TypeCaseHelper.convert(getCurrentTime(), "Timestamp", "yyyy-MM-dd HH:mm:ss");
if (obj != null)
return (Timestamp) obj;
else
return null;
}
public static Date stringToDate(String strDate, String srcDateFormat, String dstDateFormat) {
Date rtDate = null;
Date tmpDate = (new SimpleDateFormat(srcDateFormat)).parse(strDate, new ParsePosition(0));
String tmpString = null;
if (tmpDate != null)
tmpString = (new SimpleDateFormat(dstDateFormat)).format(tmpDate);
if (tmpString != null)
rtDate = (new SimpleDateFormat(dstDateFormat)).parse(tmpString, new ParsePosition(0));
return rtDate;
}
public static float dp2px(Context context, int dpVal) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}
public static float sp2px(Context context, float spVal) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, context.getResources().getDisplayMetrics());
}
public static int getDisplayWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
public static int getDisplayHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
public static boolean isEqualsMonth(DateTime dateTime1, DateTime dateTime2) {
return dateTime1.getMonthOfYear() == dateTime2.getMonthOfYear();
}
public static boolean isLastMonth(DateTime dateTime1, DateTime dateTime2) {
DateTime dateTime = dateTime2.plusMonths(-1);
return dateTime1.getMonthOfYear() == dateTime.getMonthOfYear();
}
public static boolean isNextMonth(DateTime dateTime1, DateTime dateTime2) {
DateTime dateTime = dateTime2.plusMonths(1);
return dateTime1.getMonthOfYear() == dateTime.getMonthOfYear();
}
public static int getIntervalMonths(DateTime dateTime1, DateTime dateTime2) {
return (dateTime2.getYear() - dateTime1.getYear()) * 12 + (dateTime2.getMonthOfYear() - dateTime1.getMonthOfYear());
}
public static int getIntervalWeek(DateTime dateTime1, DateTime dateTime2) {
DateTime sunFirstDayOfWeek1 = getSunFirstDayOfWeek(dateTime1);
DateTime sunFirstDayOfWeek2 = getSunFirstDayOfWeek(dateTime2);
int days = Days.daysBetween(sunFirstDayOfWeek1, sunFirstDayOfWeek2).getDays();
if (days > 0) {
return (days + 1) / 7;
} else if (days < 0) {
return (days - 1) / 7;
} else {
return days;
}
}
public static boolean isToday(DateTime dateTime) {
return new DateTime().toLocalDate().equals(dateTime.toLocalDate());
}
public static int getFirstDayOfWeekOfMonth(int year, int month) {
int dayOfWeek = new DateTime(year, month, 1, 0, 0, 0).getDayOfWeek();
if (dayOfWeek == 7) {
return 0;
}
return dayOfWeek;
}
public static DateTime getSunFirstDayOfWeek(DateTime dateTime) {
if (dateTime.dayOfWeek().get() == 7) {
return dateTime;
} else {
return dateTime.minusWeeks(1).withDayOfWeek(7);
}
}
public static int getMonthDays(int year, int month) {
month++;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
return 29;
} else {
return 28;
}
default:
return -1;
}
}
public static int getMonthRows(int year, int month) {
int size = getFirstDayOfWeekOfMonth(year, month) + getMonthDays(year, month) - 1;
return size % 7 == 0 ? size / 7 : (size / 7) + 1;
}
public static int getFirstDayWeek(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, 1);
return calendar.get(Calendar.DAY_OF_WEEK);
}
public static int getYearByTimeStamp(long timeStamp) {
String date = timeStampToDate(timeStamp);
String year = date.substring(0, 4);
return Integer.parseInt(year);
}
public static String timeStampToDate(long timeStamp) {
Date date = new Date(timeStamp);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = simpleDateFormat.format(date);
return dateStr;
}
public static Integer getWeekOfDate(DateTime dt) {
Integer[] weekDays = {0, 1, 2, 3, 4, 5, 6};
Calendar cal = Calendar.getInstance();
cal.setTime(dt.toDate());
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
public static Date addDate(Date d, long day) {
long time = d.getTime();
day = day * 24 * 60 * 60 * 1000;
time += day;
return new Date(time);
}