![]() |
|
||||||||||||||
| | 网站首页 | 数据库教程 | web编程 | 服务器 | 程序设计 | | ||
|
||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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' 转换定义的。
以下转换字符用来格式化时间:
以下转换字符用来格式化日期:
以下转换字符用于格式化常见的日期/时间组合。
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 网站地图 | 管理登录 | | |||
|