网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> Java编程 >> 文章正文
  java Dates &Times完全总结            【字体:
java Dates &Times完全总结
作者:佚名    文章来源:不详    点击数:    更新时间:2007-9-2    
1.获得当前时间的方法:
利用java.until.Date
System.err.print("Formant 1: ");
  System.out.println(new Date(System.currentTimeMillis()) );
  //System.out.print("The 2 formant: ");
  
  
  System.err.print("Formant 2: ");
  Date d=new Date();
  long t=d.getTime();
  System.out.println(new Date(t));
  
  //System.out.println(d.toLocaleString());  
  
  System.err.print("Formant 3:");
  Calendar date=Calendar.getInstance();
  date.add(Calendar.DATE, 0);//当前的前一天
  System.out.println(date.getTime());
利用java.until.Calendar
// Get a Calendar for current locale and time zoneCalendar cal = Calendar.getInstance(); // Figure out what day of the year today iscal.setTimeInMillis(System.currentTimeMillis()); // Set to the current timeint dayOfYear = cal.get(Calendar.DAY_OF_YEAR); // What day of the year is it?// What day of the week does the leap day in the year 2008 occur on?cal.set(2008, Calendar.FEBRUARY, 29); // Set year, month, day fieldsint dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); // Query a different field// What day of the month is the 3rd Thursday of May, 2005?cal.set(Calendar.YEAR, 2005); // Set the yearcal.set(Calendar.MONTH, Calendar.MAY); // Set the monthcal.set(Calendar.DAY_OF_WEEK,Calendar.THURSDAY); // Set the day of weekcal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 3); // Set the weekint dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); // Query the day in month// Get a Date object that redivsents three months from nowcal.setTimeInMillis(System.currentTimeMillis()); // Current timecal.add(Calendar.MONTH, 3); // Add 3 monthsDate expiration = cal.getTime(); // Retrieve result as a Datelong millis = cal.getTimeInMillis(); // or get it as a long
 
2.时间的格式:
// current hours and minuteslong now = System.currentTimeMillis();String s = String.format("%tR", now); // "15:12"// Current month/day/yearDate d = new Date(now);s = String.format("%tD", d); // "07/13/04"// Hours and minutes using 12-hour clockCalendar c = Calendar.getInstance();c.setTime(d);s = String.format("%tl:%tM %tp", now, d, c); // "3:12 pm"
 
时间格式的转换

日期/时间转换

以下日期和时间转换的后缀字符是为 't''T' 转换定义的。
正在装载数据……
这些类型相似于但不完全等同于那些由 GNU date 和 POSIX strftime(3c) 定义的类型。提供其他转换类型是为了访问特定于 Java 的功能(如将 'L' 用作秒中的毫秒)。

以下转换字符用来格式化时间:
'H' 24 小时制的小时,被格式化为必要时带前导零的两位数,即 00 - 23
'I' 12 小时制的小时,被格式化为必要时带前导零的两位数,即 01 - 12
'k' 24 小时制的小时,即 0 - 23
'l' 12 小时制的小时,即 1 - 12
'M' 小时中的分钟,被格式化为必要时带前导零的两位数,即 00 - 59
'S' 分钟中的秒,被格式化为必要时带前导零的两位数,即 00 - 60 ("60" 是支持闰秒所需的一个特殊值)。
'L' 秒中的毫秒,被格式化为必要时带前导零的三位数,即 000 - 999
'N' 秒中的毫微秒,被格式化为必要时带前导零的九位数,即 000000000 - 999999999
'p' 特定于语言环境的 上午或下午 标记以小写形式表示,例如 "am" 或 "pm"。使用转换前缀 'T' 可以强行将此输出转换为大写形式。
'z' 相对于 GMT 的 RFC 822 格式的数字时区偏移量,例如 -0800
'Z' 表示时区缩写形式的字符串。Formatter 的语言环境将取代参数的语言环境(如果有)。
's' 自协调世界时 (UTC) 1970 年 1 月 1 日 00:00:00 至现在所经过的秒数,即 Long.MIN_VALUE/1000Long.MAX_VALUE/1000 之间的差值。
'Q' 自协调世界时 (UTC) 1970 年 1 月 1 日 00:00:00 至现在所经过的毫秒数,即 Long.MIN_VALUELong.MAX_VALUE 之间的差值。

以下转换字符用来格式化日期:
'B' 特定于语言环境的月份全称,例如 "January""February"
'b' 特定于语言环境的月份简称,例如 "Jan""Feb"
'h' 'b' 相同。
'A' 特定于语言环境的星期几全称,例如 "Sunday""Monday"
'a' 特定于语言环境的星期几简称,例如 "Sun""Mon"
'C' 除以 100 的四位数表示的年份,被格式化为必要时带前导零的两位数,即 00 - 99
'Y' 年份,被格式化为必要时带前导零的四位数(至少),例如,0092 等于格里高利历的 92 CE。
'y' 年份的最后两位数,被格式化为必要时带前导零的两位数,即 00 - 99
'j' 一年中的天数,被格式化为必要时带前导零的三位数,例如,对于格里高利历是 001 - 366
'm' 月份,被格式化为必要时带前导零的两位数,即 01 - 13
'd' 一个月中的天数,被格式化为必要时带前导零两位数,即 01 - 31
'e' 一个月中的天数,被格式化为两位数,即 1 - 31

以下转换字符用于格式化常见的日期/时间组合。
'R' 24 小时制的时间,被格式化为 "%tH:%tM"
'T' 24 小时制的时间,被格式化为 "%tH:%tM:%tS"
'r' 12 小时制的时间,被格式化为 "%tI:%tM:%tS %Tp"。上午或下午标记 ('%Tp') 的位置可能与语言环境有关。
'D' 日期,被格式化为 "%tm/%td/%ty"
'F' ISO 8601 格式的完整日期,被格式化为 "%tY-%tm-%td"
'c' 日期和时间,被格式化为 "%ta %tb %td %tT %tZ %tY",例如 "Sun Jul 20 16:17:00 EDT 1969"

 3.使用DateFormat

import java.util.Date;import java.text.*;// Display today's date using a default format for the current localeDateFormat defaultDate = DateFormat.getDateInstance();System.out.println(defaultDate.format(new Date()));// Display the current time using a short time format for the current localeDateFormat shortTime = DateFormat.getTimeInstance(DateFormat.SHORT);System.out.println(shortTime.format(new Date()));// Display date and time using a long format for bothDateFormat longTimestamp = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);System.out.println(longTimestamp.format(new Date()));// Use SimpleDateFormat to define your own formatting template// See java.text.SimpleDateFormat for the template syntaxDateFormat myformat = new SimpleDateFormat("yyyy.MM.dd"); System.out.println(myformat.format(new Date()));try { // DateFormat can parse dates too Date leapday = myformat.parse("2000.02.29"); }catch (ParseException e) { /* Handle parsing exception */ }



本文来源:http://blog.csdn.net/baobeiSimple/archive/2007/07/16/1693984.aspx
站内文章搜索 高级搜索
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     用java实现web服务器
     用java快速开发linux gu…
     正则表达式分解siemens交…
     [portal参考手册]目录
     jsp中调用oracle存储过程…
  • page、request、session、ap…

  • JSP中JavaBean的生命周期

  • Java Swing实现俄罗斯方块

  • 快速、简便使用AJAX技术的三…

  • java异常处理机制的深入理解

  • [转]Java堆和栈的区别 经典总…

  • 关于java Applet

  • java 设计工厂模式

  • Core Java 之旅

  • 专访Java之父:Java未来的发…

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David