Date类
在之前一直在强调简单Java类的概念,也重点阐述了简单Java类和数据表之间的映射关系的,但是对于数据表的日期型字段却一直没有映射,而在Java之中,如果要想表示出日期型,则使用java.util.Date类完成。
No. | 方法 | 类型 | 描述 |
1 | public Date() | 构造 | 实例化Date类对象 |
2 | public Date(long date) | 构造 | 将数字变为Date类对象,long为日期时间数据 |
3 | public long getTime() | 普通 | 将当前的日期时间变为long型 |
取得当前的日期时间
import java.util.Date;
public class TestDemo {
public static void main(String[] args) throws Exception {
Date date = new Date();
System.out.println(date); // 输出对象信息
}
}
Date与long间的转换
import java.util.Date;
public class TestDemo {
public static void main(String[] args) throws Exception {
long cur = System.currentTimeMillis(); // 取得当前的日期时间以long型返回
Date date = new Date(cur); // 将long转换为Date
System.out.println(date); // 输出对象信息
System.out.println(date.getTime()); // 输出对象信息
}
}
程序执行结果:
2018-02-06
1517901876467
日期格式化:SimpleDateFormat
虽然使用java.util.Date类可以明确的取得当前的日期时间,但是最终数据的显示格式并不方便阅读。如果要对显示的日期时间进行格式转换,则可以通过java.text.SimpleDateFormat类完成
No. | 方法 | 类型 | 描述 |
1 | public SimpleDateFormat(String pattern) | 构造 | 传入日期时间标记实例化对象 |
2 | public final String format(Date date) | 普通 | 将日期格式化为字符串数据 |
3 | public Date parse(String source) throws ParseException | 普通 | 将字符串格式化为日期数据 |
将日期格式化显示(Date型数据变为了String型数据)
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDemo {
public static void main(String[] args) throws Exception {
Date date = new Date(); // 实例化Date类对象
// 实例化SimpleDateFormat类对象,同时定义好要转换的目标字符串格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String str = sdf.format(date); // 将Date型变为了String型
System.out.println(str);
}
}
将字符串转换为日期
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDemo {
public static void main(String[] args) throws Exception {
String str = "2005-07-27 07:15:22.111" ; // 字符串由日期时间组成
// 实例化SimpleDateFormat类对象,同时定义好要转换的目标字符串格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;
Date date = sdf.parse(str) ; // 将字符串变为日期型数据
System.out.println(date);
}
}
Calendar类
Calendar类可以将取得的时间精确到毫秒,并且由于其可以分别取得日期时间数字,这样可以直接进行各种日期时间的计算操作No. 常量及方法 类型 描述
1 public static final int YEAR 常量 取得年、int类型
2 public static final int MONTH 常量 取得月、int类型
3 public static final int DAY_OF_MONTH 常量 取得日、int类型
4 public static final int HOUR_OF_DAY 常量 取得小时,24小时制、int类型
5 public static final int MINUTE 常量 取得分、int类型
6 public static final int SECOND 常量 取得秒、int类型
7 public static final int MILLISECOND 常量 取得毫秒、int类型
8 public static Calendar getInstance() 普通 根据默认的时区实例化对象
9 public boolean after(Object when) 普通 判断一个日期是否在指定日期之后
10 public boolean before(Object when) 普通 判断一个日期是否在指定日期之前
11 public int get(int field) 普通 返回给定日历字段的值
取得当前的日期时间
import java.util.Calendar;
public class TestDemo {
public static void main(String[] args) throws Exception {
Calendar cal = Calendar.getInstance(); // 取得本类对象
StringBuffer buf = new StringBuffer(); // 保存日期时间数据
buf.append(cal.get(Calendar.YEAR)).append("-"); // 取得年数据
buf.append(cal.get(Calendar.MONTH) + 1).append("-"); // 取得月数据,从0开始
buf.append(cal.get(Calendar.DAY_OF_MONTH)).append(" "); // 取得天数据
buf.append(cal.get(Calendar.HOUR_OF_DAY)).append(":"); // 取得小时数据
buf.append(cal.get(Calendar.MINUTE)).append(":"); // 取得分钟数据
buf.append(cal.get(Calendar.SECOND)); // 取得秒数据
System.out.println(buf);
}
}
取得五天之后的日期
import java.util.Calendar;
public class TestDemo {
public static void main(String[] args) throws Exception {
Calendar cal = Calendar.getInstance(); // 取得本类对象
StringBuffer buf = new StringBuffer(); // 保存日期时间数据
buf.append(cal.get(Calendar.YEAR)).append("-"); // 取得年数据
buf.append(cal.get(Calendar.MONTH) + 1).append("-"); // 取得月数据,从0开始
buf.append(cal.get(Calendar.DAY_OF_MONTH) + 5).append(" ");// 取得5天后数据
buf.append(cal.get(Calendar.HOUR_OF_DAY)).append(":"); // 取得小时数据
buf.append(cal.get(Calendar.MINUTE)).append(":"); // 取得分钟数据
buf.append(cal.get(Calendar.SECOND)); // 取得秒数据
System.out.println(buf);
}
}