SimpleDateFormat三個重要的方法
- 構造方法 public SimpleDateFormat(String pattern)
- 將字符串轉換爲Date方法 public Date parse(String source) throws ParseException
- 將Date類型轉換爲指定格式的String類型 public final String format(Date date)
- package com.lee.mxdx;
- import java.text.SimpleDateFormat;
- import java.util.*;
-
-
- class DateTime{
- Date sysDate = new Date();
- private final static String pat1 = "yyyy-MM-dd HH:mm:ss.SS";
- private final static String pat2 = "yyyy年MM月dd日 HH時mm分ss秒SS毫秒";
- private final static String pat3 = "yyyyMMddHHmmssSS";
-
- public String getDate(){
- SimpleDateFormat sdf = new SimpleDateFormat(pat1);
- String strDate= sdf.format(sysDate);
- return strDate;
- }
-
- public String getDateComplete(){
- SimpleDateFormat sdf = new SimpleDateFormat(pat2);
- String strDate= sdf.format(sysDate);
- return strDate;
- }
-
- public String getTimeStamp(){
- SimpleDateFormat sdf = new SimpleDateFormat(pat3);
- String strDate= sdf.format(sysDate);
- return strDate;
- }
- }
-
-
-
-
- public class DateDemo {
- public static void main(String[] args) {
- DateTime dateTime = new DateTime();
- System.out.println(dateTime.getDate());
- System.out.println(dateTime.getDateComplete());
- System.out.println(dateTime.getTimeStamp());
- }
- }
輸出結果:
2018-03-27 23:10:59.196
2018年03月27日 23時10分59秒196毫秒
20180327231059196