日期格式化类 DateFormatUtils
Maven依赖
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency>
常用API
Date date = new Date(); // 格式化Date类型时间 String dateStr = DateFormatUtils.format(date, "yyyy-MM-dd"); long time = date.getTime(); // 格式化long类型时间 String dateStr1 = DateFormatUtils.format(time, "yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); // 格式化Calendar类型时间 String dateStr2 = DateFormatUtils.format(calendar, "yyyy-MM-dd");
/** * 方法名:getDay * 描述:获取今天日期 */ public static String getDay(){ Calendar calendar = Calendar.getInstance(); String date=DateFormatUtils.format(calendar, "yyyyMMddHHmmss"); return date; }
public class Helper { // 当前的时间 public static String nowDateTime() { SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(); return ft.format(date); } // 当前的时间 public static String nowDateStringTime() { SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddHHmmss"); Date date = new Date(); return ft.format(date); } public static String formatDate(String dateStr) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = sdf.parse(dateStr); } catch (Exception e) { e.printStackTrace(); } SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); String format = sf.format(date); return format; } public static String transferDateTime(String dateStr) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm"); Date date = null; try { date = sdf.parse(dateStr); } catch (Exception e) { e.printStackTrace(); } SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); String format = sf.format(date); return format; } public static void main(String[] args) { nowDateStringTime(); } }
故乡明