Java裡Date類的parse和format方法
阿新 • • 發佈:2018-12-18
Date date = new Date(); //獲取當前系統時間,格式為: Thu Nov 01 18:27:06 CST 2018
format: 按指定的目標格式把Date物件轉換為String
parse: 按指定的源格式把String轉換為Date物件
public class test { public static void main(String[] args) throws ParseException { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String dateFormat = sdf.format(date); Date dateFormatParse = sdf.parse(dateFormat); System.out.println("date: "+date); System.out.println("date.toString: "+date.toString()); System.out.println("dateFormat: "+dateFormat); System.out.println("dateFormatParse "+dateFormatParse); } }
輸出:
date: Thu Nov 01 18:27:06 CST 2018 date.toString: Thu Nov 01 18:27:06 CST 2018 dateFormat: 2018-11-01 06:27:06 dateFormatParse Thu Nov 01 06:27:06 CST 2018