我们将以Java自带的时间日期类和当中的处理函数进行分析。

一、与时间日期有关的类。

  java.util.Date。

实现类,其对象具有时间、日期组件。

  java.util.Calendar。

抽象类。其对象具有时间、日期组件。

  java.sql.Date。

实现类,其对象具有日期组件。

  java.sql.Time。实现类,其对象具有时间组件。

  java.sql.Timestamp。实现类,其对象具有时间日期组件。

  java.text.DateFormat。

抽象类,其对象格式化时间日期。

我们通常会使用的类java.util.Date、java.util.Calendar、java.sql.Timestamp、java.text.DateFormat进行时间日期操作,由于他们有全然的时间日期组件和全面的格式化功能。

值得注意的是java.sql.Date没有时间组件,而java.sql.Time没有日期组件!

以下是使用方法举例

public static void main(String[] args) {
/**
* 展示各个日期时间组件的输出类型
*/
java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
System.out.println(sqlDate.toString()); // 输出结果:2015-06-25 java.sql.Time sqlTime = new java.sql.Time(System.currentTimeMillis());
System.out.println(sqlTime.toString()); // 输出结果:09:13:43 java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(System.currentTimeMillis());
System.out.println(sqlTimestamp.toString()); // 输出结果:2015-06-25 09:13:43.561 java.util.Date utilDate = new java.util.Date(System.currentTimeMillis());
System.out.println(utilDate.toString()); // 输出结果:Thu Jun 25 09:13:43 CST 2015 java.util.Calendar cl = java.util.Calendar.getInstance();
System.out.println(cl.getTime().toString()); // 输出结果:Thu Jun 25 09:13:43 CST 2015
}

public static void main(String[] args) {
/**
* java.sql.Timestamp的使用方法。
*/
@SuppressWarnings("deprecation")
java.sql.Timestamp sqlTimestamp1 = new java.sql.Timestamp(2015,6,25,9,13,43,561);
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(System.currentTimeMillis());
System.out.println(sqlTimestamp); Boolean isAfter = sqlTimestamp.after( sqlTimestamp1 );
System.out.println( isAfter ); //false Boolean isBefore = sqlTimestamp.before( sqlTimestamp1 );
System.out.println( isBefore ); //true
//用于比較两个时间点的长度,单位毫秒
Long gettimes = sqlTimestamp.getTime();
System.out.println( gettimes ); //输出 :1435196988250
//将字符串类型转为 Timestamp 类型
java.sql.Timestamp sqlTimestamp2 = java.sql.Timestamp.valueOf( "2015-06-25 09:48:16.524" );
System.out.println( sqlTimestamp2 ); //输出:2015-06-25 09:48:16.524
}
public static void main(String[] args) {
/**
* java.text.SimpleDateFormat的使用方法
*/
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp now = new Timestamp(System.currentTimeMillis());
System.out.println(now); // 2015-06-25 14:27:41.477
String time = df.format(now);
System.out.println(time); // 2015-06-25 14:27:41
System.out.println(Timestamp.valueOf(time)); // 2015-06-25 14:27:41.0 Date now1 = new Date();
System.out.println(now); // 2015-06-25 14:27:41.477
String time1 = df.format(now1);
System.out.println(time1); // 2015-06-25 14:27:41
System.out.println(Timestamp.valueOf(time1)); // 2015-06-25 14:27:41.0 }

public static void main(String[] args) {
/**
* java.util.Calendar的使用方法
*/
Calendar calendar1 = Calendar.getInstance();
System.out.println(calendar1); // java.util.GregorianCalendar[time=1435214975097,areFieldsSet=true,
// areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",
// offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],
// firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2015,MONTH=5,
// WEEK_OF_YEAR=26,WEEK_OF_MONTH=4,DAY_OF_MONTH=25,DAY_OF_YEAR=176,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,
// AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=49,SECOND=35,MILLISECOND=97,ZONE_OFFSET=28800000,DST_OFFSET=0] // 获取时间
Date date1 = calendar1.getTime();
System.out.println(date1); // Thu Jun 25 14:49:35 CST 2015
System.out.println(calendar1.getWeeksInWeekYear()); // 52
System.out.println(calendar1.get(Calendar.DAY_OF_MONTH)); // 25
System.out.println(calendar1.get(Calendar.HOUR_OF_DAY)); // 14 }

最新文章

  1. Java数据结构——图
  2. Ubuntu 16.04系统布署小记
  3. LINUX下成功搭建SVN
  4. nat模式、路由模式,网桥模式
  5. schedule()函数的调用时机(周期性调度)
  6. MySQL的create table as 与 like区别(转)
  7. POJ 3494 Largest Submatrix of All 1’s
  8. 301、302、200、206、304、404等HTTP状态引见(转载)
  9. 30多个Android 开发者工具 带你开发带你飞
  10. 《深入浅出MySQL》之数据类型
  11. jq 中each的用法 (share)
  12. final等关键字和代码块
  13. 【转】Android中保持Service的存活
  14. NOIP2015提高组复赛B 子串
  15. 一些oracle的经验
  16. Change upload file limit in specified Webapllication in SharePoint (PowerShell)
  17. python程序打包
  18. Java中统计字符串中各个字符出现的次数
  19. 1.Variables-变量(Dart中文文档)
  20. .net 控件生命周期

热门文章

  1. python-高级编程-05-异步IO
  2. 使用SpringMVC参数传递时,解决get请求时中文乱码的问题
  3. [java开发篇][dom4j模块] 遍历xml文件
  4. uiautomator 一个简单脚本创建流程
  5. POJ 1315 Don't Get Rooked
  6. [luoguP2761] 软件补丁问题(状压最短路)
  7. poj1324 Holedox Moving
  8. luogu 1355 神秘大三角 判断点和三角形的位置关系 面积法 叉积法
  9. serializeObject 的应用
  10. hdu 3081 hdu 3277 hdu 3416 Marriage Match II III IV //最大流的灵活运用