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
本文通过一个Java示例程序介绍了如何使用SimpleDateFormat类来格式化日期和时间,包括构造方法、转换方法等,并提供了三种不同的日期时间格式。
245

被折叠的 条评论
为什么被折叠?



