import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeUtilSDF {
//
public static void main(String[] args) {
TimeUtilSDF.getNextDay();
System.out.println("当前时间年月日时分秒:" + formatDateToString(new Date()));
System.out.println("当前时间年月日:" + formatDateYmdToString(new Date()));
}
//当前时间加14天后返回的String类型格式为yyyyMMddHHmmss
public static String getNextDay() {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, +14);//+14今天的时间加一天
date = calendar.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(date);
System.out.println("dateString:" + dateString);
String over_time = getTimeFront(dateString);
System.out.println("over_time:" + over_time);
return over_time;
}
// 字符串yyyyMMddHHmmss格式 转化成 字符串 yyyy-MM-dd HH:mm:ss
public static String getDateTime(String uploadTimeStr) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date uploadTime = null;
try {
uploadTime = sdf.parse(uploadTimeStr);
} catch (Exception e) {
e.printStackTrace();
}
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String upload = sdf.format(uploadTime);
return upload;
}
// 字符串 yyyy-MM-dd 格式 转化成 字符串yyyyMMddHHmmss
public static String getTime(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String timeStr = null;
try {
Date timeDate = sdf.parse(time);
sdf = new SimpleDateFormat("yyyyMMddHHmmss");
timeStr = sdf.format(timeDate);
} catch (ParseException e) {
e.printStackTrace();
}
return timeStr;
}
//字符串 yyyy-MM-dd HH:mm:ss格式 转化成 字符串yyyyMMddHHmmss
public static String getTimeFront(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeStr = null;
try {
Date timeDate = sdf.parse(time);
sdf = new SimpleDateFormat("yyyyMMddHHmmss");
timeStr = sdf.format(timeDate);
} catch (ParseException e) {
e.printStackTrace();
}
return timeStr;
}
//Date类型转化成yyyyMMddHHmmss
public static String formatDateToString(Date timeDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(timeDate);
}
//Date类型转成yyyyMMdd
public static String formatDateYmdToString(Date timeDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(timeDate);
}
}
输出结果为: