Java工具类 ``` /** * 工具类 * 取时间的各种算法(Date,String) * 时间计算方法(加减/毫秒) * @author SLG * */ public class DateTools { private static final Logger logger = Logger.getLogger(DateTools.class); /** * 将时间格式(Date)转换为时间字符串 yyyy-MM-dd HH:mm:ss(String) * Date-->String * 当前时间可传入new Date() * @param dateDate * @return */ public static String dateToStr(Date dateDate){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = format.format(dateDate); return dateString; } /** * 将短时间格式字符串(String)转换为时间 yyyy-MM-dd HH:mm:ss(Date) * String-->Date * @param strDate * @return 如Sat Jun 17 13:20:59 CST 2017 * @throws Exception */ public static Date strToDate(String strDate){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { //解析此类数据:2001-12-17T09:30:47.0Z if(strDate == null || "".equals(strDate)){ return new Date(); } if(strDate.contains("T")){ strDate = strDate.replace("T", " ").substring(0,19); } Date strtodate = format.parse(strDate); return strtodate; } catch (Exception e) { logger.error("Input strDate:"+strDate); } return null; } /** * 将格式正确的时间字符串输出成固定格式的字符串(时:分:秒) * @param str * @return * @throws Exception */ public static String strToStrDay(String str) throws Exception{ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); String dateString = sdf.format(strToDate(str)); return dateString; } /** * 将格式正确的时间输出成固定格式的字符串(时:分:秒) * @param date * @return * @throws Exception */ public static String strToStrDay(Date date) throws Exception{ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); String dateString = sdf.format(date); return dateString; } /** * 将格式正确的时间字符串输出成固定格式的字符串(年/月/日) * @param str * @return * @throws Exception */ public static String strToDay(String str) throws Exception{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); String dateString = sdf.format(strToDate(str)); return dateString; } /** * 将格式正确的时间输出成固定格式的字符串(年/月/日) * @param date * @return * @throws Exception */ public static String strToDay(Date date) throws Exception{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); String dateString = sdf.format(date); return dateString; } /** * 将格式正确的时间输出成固定格式的字符串(年月日) * @param date * @return * @throws Exception */ public static String strToDayAll(Date date){ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); String dateString = sdf.format(date); return dateString; } /** * 将格式正确的时间输出成固定格式的字符串(年月日) * @param date * @return * @throws Exception */ public static String strToTime(Date date){ SimpleDateFormat sdf = new SimpleDateFormat("HHmmss"); String dateString = sdf.format(date); return dateString; } /** * 将时间生成字符串(用于生成路径) * @param string * @return */ public static String DateToStrToRoute(String string){ String str = string.replaceAll("-", ""); str = str.replaceAll(" ",""); str = str.replaceAll(":",""); return str; } public static String YesterDay(){ Calendar calendar1 = Calendar.getInstance(); calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH), 0, 0, 0); Date beginOfDate = calendar1.getTime(); return dateToStr(beginOfDate); } public static void main(String[] args) throws Exception{ System.out.print(YesterDay()); } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up