在项目中,我们经常需要使用date和string进行转换,方便向服务器传送或获取数据,在前端也需要对前后一天进行切换。
该工具类是在项目中对几种转换进行了简易封装,包括获取当前年月日、时间戳和string的相互转换、前后一天时间计算、两个日期的大小。
import com.prolificinteractive.materialcalendarview.CalendarDay;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* 日历/日期相关转换类
*/
public class CalendarDateUtil {
/**
* 获取当前日期,年月日
* 格式:2020-12-04
*
* @return
*/
public static String getCurrentDate() {
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
StringBuilder currentDate = new StringBuilder();
String year = String.valueOf(cal.get(Calendar.YEAR));
String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
String day = String.valueOf(cal.get(Calendar.DATE));
if((cal.get(Calendar.MONTH) + 1) < 10){
month = "0" + month;
}
if (cal.get(Calendar.DATE) < 10){
day = "0" + day;
}
return currentDate.append(year + "-" + month + "-" +day).toString();
}
/**
* 获取当前日期,年月日
* 格式:2020年12月04日
*
* @return
*/
public static String getCurrentDates() {
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
StringBuilder currentDate = new StringBuilder();
String year = String.valueOf(cal.get(Calendar.YEAR));
String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
String day = String.valueOf(cal.get(Calendar.DATE));
if((cal.get(Calendar.MONTH) + 1) < 10){
month = "0" + month;
}
if (cal.get(Calendar.DATE) < 10){
day = "0" + day;
}
return currentDate.append(year + "年" + month + "月" +day+"日").toString();
}
/*
* 将时间戳转换为时间
*
* s就是时间戳
*/
public static String stampToDate(String s) {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
//如果它本来就是long类型的,则不用写这一步
long lt = new Long(s);
Date date = new Date(lt*1000);
res = simpleDateFormat.format(date);
return res;
}
/**
* 计算上/下一天
* day 当前日期
* upOrDownTag = back 为当前日期的前一天,其他为后一天
* nextNum 上/下几天
*
* @param currentDay
* @param upOrDownTag
* @param nextNum
* @return
*/
public static String upDonwDate(String currentDay, String upOrDownTag, int nextNum) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date nowDate = null;
try {
nowDate = df.parse(currentDay);
} catch (ParseException e) {
e.printStackTrace();
}
//如果需要向后计算日期 -改为+
if ("back".equals(upOrDownTag)) {
//向前计算日期
nowDate = new Date(nowDate.getTime() - (long) nextNum * 24 * 60 * 60 * 1000);
} else {
//向后计算日期
nowDate = new Date(nowDate.getTime() + (long) nextNum * 24 * 60 * 60 * 1000);
}
return df.format(nowDate);
}
/**
* 选中日历选择器,返回日期
*
* @param date
* @return
*/
public static String getSelectDay(CalendarDay date) {
StringBuilder selectDay = new StringBuilder();
String year = date.getYear() + "";
String month = (date.getMonth() + 1) +"";
String day = "";
if (date.getDay() < 10) {
day = "0" + date.getDay();
} else {
day = date.getDay() + "";
}
if ( (date.getMonth() + 1) < 10) {
month = "0" + month;
} else {
month = month + "";
}
selectDay.append(year + "-" + month + "-" + day);
return selectDay.toString();
}
/**
* 比较两个日期大小
* 如果currenday 在 DATE1 之后,返回true
*
* @param currenday
* @param DATE1
* @return
*/
public static boolean compare_date(String currenday, String DATE1) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date dt1 = df.parse(currenday);
Date dt2 = df.parse(DATE1);
if (dt1.getTime() < dt2.getTime()) {
return false;
}else{
return true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return false;
}
/**
* 将指定日期转为时间戳
* @param dateString
* @return
*/
public static long getStringToDate(String dateString) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
try{
date = dateFormat.parse(dateString);
} catch(ParseException e) {
e.printStackTrace();
}
return date.getTime() / 1000;
}
/**
* 和上一个方法相对应
* 将指定日期转为时间戳
* @param dateString
* @return
*/
public static String formateQueryDate(String dateString) {
String res = "";
if (!"".equals(dateString)){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
long day = Long.parseLong(dateString);
Date date = new Date(day * 1000);
res = simpleDateFormat.format(date);}else{
}
return res;
}
/**
*将String日期转为Date,如"2020-12-10"
* @param date
* @return
* @throws ParseException
*/
public static Date parseStringToDate(String date) throws ParseException {
Date result = null;
String parse = date;
parse = parse.replaceFirst("^[0-9]{4}([^0-9]?)", "yyyy$1");
parse = parse.replaceFirst("^[0-9]{2}([^0-9]?)", "yy$1");
parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1MM$2");
parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}( ?)", "$1dd$2");
parse = parse.replaceFirst("( )[0-9]{1,2}([^0-9]?)", "$1HH$2");
parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1mm$2");
parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1ss$2");
SimpleDateFormat format = new SimpleDateFormat(parse, Locale.CHINA);
result = format.parse(date);
return result;
}
}