Java获取当前时间年月日、时间格式化打印、字符串转日期
package com.sysc.simple; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtils { public static void show() throws ParseException { //获取当前日期 Calendar now = Calendar.getInstance(); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) + 1;//注意月份 int day = now.get(Calendar.DAY_OF_MONTH); int hour = now.get(Calendar.HOUR_OF_DAY); int minute = now.get(Calendar.MINUTE); int second = now.get(Calendar.SECOND); long millis = now.getTimeInMillis(); System.out.println(now.getTime()); System.out.println(year + month + day + hour + minute + second + millis); //日期格式化打印 Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateNowStr = sdf.format(d); System.out.println("格式化后的日期:" + dateNowStr); //字符串解析为日期 String str = "2014-9-1 13:08:28"; Date today = sdf.parse(str); System.out.println("字符串转成日期:" + today); } }