标签:
一、日期和long类型数据的相互转换
public class Hello { public static void main(String[] args) throws Exception { //日期转换为long Date currentDateInDate = new Date(); long currentDateInLong = currentDateInDate.getTime(); System.out.println(currentDateInDate); System.out.println(currentDateInLong); } }
二、long类型转换为日期
public class Hello { public static void main(String[] args) throws Exception { //日期转换为long long currentDateInLong = System.currentTimeMillis(); Date currentDateInDate = new Date(currentDateInLong); System.out.println(currentDateInDate); System.out.println(currentDateInLong); } }
三、日期格式化(将日期format成字符串)
public class Hello { public static void main(String[] args) throws Exception { //日期转换为long long currentDateInLong = System.currentTimeMillis(); Date currentDateInDate = new Date(currentDateInLong); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS"); String currentDateInString = sdf.format(currentDateInDate); System.out.print(currentDateInString); } }
四、日期的解析(将字符串转化为日期)
public class Hello { public static void main(String[] args) throws Exception { String currentDateInString = "2016-08-07 19:24:03.0528"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS"); Date currentDateInDate = sdf.parse(currentDateInString); System.out.print(currentDateInDate); } }
五、利用Calendar类获取日期的各个部分
public class Hello { public static void main(String[] args) throws Exception { Calendar calendar = Calendar.getInstance(); System.out.println("年:" + calendar.get(Calendar.YEAR)); System.out.println("月:" + (calendar.get(Calendar.MONTH) + 1)); System.out.println("日:" + calendar.get(Calendar.DAY_OF_MONTH)); } }
标签:
原文地址:http://www.cnblogs.com/kuillldan/p/5746924.html