java 中的SimpleDateFormat、Date函数以及字符串和Date类型互转

时间:2023-03-08 22:06:18

SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。

SimpleDateFormat使得可以选择任何用户定义的日期-时间格式的模式。但是,仍然建议通过DateFormat中的getTimeInstancegetDateInstancegetDateTimeInstance 来创建日期-时间格式器。每一个这样的类方法都能够返回一个以默认格式模式初始化的日期/时间格式器。可以根据需要使用applyPattern 方法来修改格式模式。

String类型的时间转换成Date类型时间

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse("2015-08-28");

Date类型的时间转换成String类型时间

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年M月d日");
String date = dateFormat.format(new Date());
Date()函数
dateA.after(dateB); //日期比较函数日期A在日期B之前 返回true或false
  dateA.before(dateB); //日期比较函数日期A在日期B之后 返回true或false
  dateA.compareTo(dateB);//比较两个日期的顺序,返回值为int -1、0、1
  dateA.equals(dateB); //比较两个日期的相等性
Boolean:after(Date when) 测试此日期是否在指定日期之后。before(Date when) 测试此日期是否在指定日期之前。
equals(Object obj) 比较两个日期的相等性。
int:
compareTo(Date anotherDate) 比较两个日期的顺序。
方法摘要
boolean after(Date when) 
测试此日期是否在指定日期之后。
boolean before(Date when) 
测试此日期是否在指定日期之前。
Object clone() 
返回此对象的副本。
int compareTo(Date anotherDate) 
比较两个日期的顺序。
boolean equals(Object obj) 
比较两个日期的相等性。
int getDate() 
已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.DAY_OF_MONTH) 取代。
int getDay() 
已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.DAY_OF_WEEK) 取代。
int getHours() 
已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.HOUR_OF_DAY) 取代。
int getMinutes() 
已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.MINUTE) 取代。
int getMonth() 
已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.MONTH) 取代。
int getSeconds() 
已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.SECOND) 取代。
long getTime() 
返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
int getTimezoneOffset() 
已过时。 从 JDK 1.1 开始,由 -(Calendar.get(Calendar.ZONE_OFFSET)
+ Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000)
 取代。
int getYear() 
已过时。 从 JDK 1.1 开始,由 Calendar.get(Calendar.YEAR)
- 1900
 取代。
int hashCode() 
返回此对象的哈希码值。
static long parse(String s) 
已过时。 从 JDK 1.1 开始,由 DateFormat.parse(String
s)
 取代。
void setDate(int
date)
 
已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.DAY_OF_MONTH,
int date)
 取代。
void setHours(int
hours)
 
已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.HOUR_OF_DAY,
int hours)
 取代。
void setMinutes(int
minutes)
 
已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.MINUTE,
int minutes)
 取代。
void setMonth(int
month)
 
已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.MONTH,
int month)
 取代。
void setSeconds(int
seconds)
 
已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.SECOND,
int seconds)
 取代。
void setTime(long
time)
 
设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。
void setYear(int
year)
 
已过时。 从 JDK 1.1 开始,由 Calendar.set(Calendar.YEAR,
year + 1900)
 取代。
String toGMTString() 
已过时。 从 JDK 1.1 开始,由 DateFormat.format(Date
date)
 取代,使用 GMT TimeZone
String toLocaleString() 
已过时。 从 JDK 1.1 开始,由 DateFormat.format(Date
date)
 取代。
String toString() 
把此 Date 对象转换为以下形式的 String
dow mon dd hh:mm:ss zzz yyyy 其中: dow 是一周中的某一天 (Sun,
Mon, Tue, Wed, Thu, Fri, Sat
)。
static long UTC(int
year, int month, int date, int hrs, int min, int sec)
 
已过时。 从 JDK 1.1 开始,由 Calendar.set(year
+ 1900, month, date, hrs, min, sec)
 或 GregorianCalendar(year + 1900, month, date, hrs, min, sec) 取代,使用
UTC TimeZone,后跟 Calendar.getTime().getTime()