Java Date, Time and Format
2012-02-20 17:51
330 查看
1. java.util.Date 类常用方法:
public Date()public Date(long date)
long getTime()
void setTime(long time)
boolean after(Date when)
boolean before(Date when)
int compareTo(Date anotherDate)
Object clone()
boolean equals(Object obj)
int hashCode()
String toString()
其他方法皆已过时。
2. 若想直接获取或者改变年、月、日、时、分、秒等等这些属性的值,用java.util.Calendar / GregorianCalendar:
Calendar calendar = Calendar.getInstance(); 或Calendar calendar = new GregorianCalendar();
Date getTime()
void setTime(Date date)
Date getTime()
void add(int field, int amount)
void roll(int field, boolean up)
void roll(int field, int amount)
推荐用常量Calendar.JULY来设置月份值,因为0是第一月
void set(int field, int value) //field: public static final int Calendar.DAY_OF_YEAR
void set(int year, int month, int date)
void set(int year, int month, int date, int hourOfDay, int minute)
void set(int year, int month, int date, int hourOfDay, int minute, int second)
GregorianCalendar()
GregorianCalendar(int year, int month, int dayOfMonth)
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute)
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second)
3. 若要格式化输出或转化:
只用DateFormat的话,DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL);
String dateStr = df.format(new Date());
用SimpleDateFormat,
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String dateStr = df.format(new Date());
Date date = df.pase(“2001-07-04T12:08:56.235-0700”);
相关文章推荐
- Java 8 Date Time API Example Tutorial – LocalDate, Instant, LocalDateTime, Parse and Format
- java convert timestamp to date and time
- ASP Format Date and Time Script
- Custom Date and Time Format Strings
- Formatting Time and Date: The Time and Date Format Suffixes
- Joda Time - Java date and time API
- Simple trick to convert Date format with timezone in Java!
- Java.SimpleDateFormat.Date.getTime.setTime.Format
- Java – Convert date and time between timezone
- [原创]DateTime在使用 format Custom Date and Time Format Strings时遇到的问题和解决方法
- Java 8系列(一): 日期/时间- JSR310( Date and Time API)
- Java Date, Calendar and Time API - Tutorial
- 5.33Java 三星题 current date and time
- 10 ways to format time and date using JavaScript
- Explain differences among java.util.Date, java.sql.Date, java.sql.Time, and java.sql.Timestamp?
- Java – Convert date and time between timezone
- DateAndTime.java
- short or long (date and time)format display
- Date and Time Format Patterns
- java.util.Date vs java.sql.Date, java.sql.Time and java.sql.Timestamp