Calendar 日历类的基本用法
来源:互联网 发布:神奈川县湘北赤木数据 编辑:程序博客网 时间:2024/05/22 05:18
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "><a target=_blank href="http://lib.csdn.net/base/17" class="replace_word" title="Java EE知识库" target="_blank" style="color: rgb(223, 52, 52); text-decoration: none; font-weight: bold; ">Java</a> Calendar 类时间操作,这也许是创建日历和管理最简单的一个方案,示范代码很简单。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; ">演示了获取时间,日期时间的累加和累减,以及比较。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; ">注意事项:</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; ">Calendar 的 month 从 0 开始,也就是全年 12 个月由 0 ~ 11 进行表示。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; ">而 Calendar.DAY_OF_WEEK 定义和值如下:</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; ">Calendar.SUNDAY = 1Calendar.MONDAY = 2Calendar.TUESDAY = 3Calendar.WEDNESDAY = 4Calendar.THURSDAY = 5Calendar.FRIDAY = 6Calendar.SATURDAY = 7</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; ">SimpleDateFormat 的格式定义</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "></p><table border="0" cellspacing="3" cellpadding="0" summary="Chart shows pattern letters, date/time component, presentation, and examples." class=" " style="color: rgb(0, 0, 0); font-family: Arial; font-size: 14px; line-height: 26px; text-align: left; "><tbody><tr style="background-color: rgb(204, 204, 255); "><th align="left">Letter</th><th align="left">Date or Time Component</th><th align="left">Presentation</th><th align="left">Examples</th></tr><tr><td><code>G</code></td><td>Era designator</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#text" style="color: rgb(202, 0, 0); text-decoration: none; ">Text</a></td><td><code>AD</code></td></tr><tr style="background-color: rgb(238, 238, 255); "><td><code>y</code></td><td>Year</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#year" style="color: rgb(202, 0, 0); text-decoration: none; ">Year</a></td><td><code>1996</code>; <code>96</code></td></tr><tr><td><code>Y</code></td><td>Week year</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#year" style="color: rgb(202, 0, 0); text-decoration: none; ">Year</a></td><td><code>2009</code>; <code>09</code></td></tr><tr style="background-color: rgb(238, 238, 255); "><td><code>M</code></td><td>Month in year (context sensitive)</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#month" style="color: rgb(202, 0, 0); text-decoration: none; ">Month</a></td><td><code>July</code>; <code>Jul</code>; <code>07</code></td></tr><tr><td><code>L</code></td><td>Month in year (standalone form)</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#month" style="color: rgb(202, 0, 0); text-decoration: none; ">Month</a></td><td><code>July</code>; <code>Jul</code>; <code>07</code></td></tr><tr style="background-color: rgb(238, 238, 255); "><td><code>w</code></td><td>Week in year</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#number" style="color: rgb(202, 0, 0); text-decoration: none; ">Number</a></td><td><code>27</code></td></tr><tr><td><code>W</code></td><td>Week in month</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#number" style="color: rgb(202, 0, 0); text-decoration: none; ">Number</a></td><td><code>2</code></td></tr><tr style="background-color: rgb(238, 238, 255); "><td><code>D</code></td><td>Day in year</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#number" style="color: rgb(202, 0, 0); text-decoration: none; ">Number</a></td><td><code>189</code></td></tr><tr><td><code>d</code></td><td>Day in month</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#number" style="color: rgb(202, 0, 0); text-decoration: none; ">Number</a></td><td><code>10</code></td></tr><tr style="background-color: rgb(238, 238, 255); "><td><code>F</code></td><td>Day of week in month</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#number" style="color: rgb(202, 0, 0); text-decoration: none; ">Number</a></td><td><code>2</code></td></tr><tr><td><code>E</code></td><td>Day name in week</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#text" style="color: rgb(202, 0, 0); text-decoration: none; ">Text</a></td><td><code>Tuesday</code>; <code>Tue</code></td></tr><tr style="background-color: rgb(238, 238, 255); "><td><code>u</code></td><td>Day number of week (1 = Monday, ..., 7 = Sunday)</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#number" style="color: rgb(202, 0, 0); text-decoration: none; ">Number</a></td><td><code>1</code></td></tr><tr><td><code>a</code></td><td>Am/pm marker</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#text" style="color: rgb(202, 0, 0); text-decoration: none; ">Text</a></td><td><code>PM</code></td></tr><tr style="background-color: rgb(238, 238, 255); "><td><code>H</code></td><td>Hour in day (0-23)</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#number" style="color: rgb(202, 0, 0); text-decoration: none; ">Number</a></td><td><code>0</code></td></tr><tr><td><code>k</code></td><td>Hour in day (1-24)</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#number" style="color: rgb(202, 0, 0); text-decoration: none; ">Number</a></td><td><code>24</code></td></tr><tr style="background-color: rgb(238, 238, 255); "><td><code>K</code></td><td>Hour in am/pm (0-11)</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#number" style="color: rgb(202, 0, 0); text-decoration: none; ">Number</a></td><td><code>0</code></td></tr><tr><td><code>h</code></td><td>Hour in am/pm (1-12)</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#number" style="color: rgb(202, 0, 0); text-decoration: none; ">Number</a></td><td><code>12</code></td></tr><tr style="background-color: rgb(238, 238, 255); "><td><code>m</code></td><td>Minute in hour</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#number" style="color: rgb(202, 0, 0); text-decoration: none; ">Number</a></td><td><code>30</code></td></tr><tr><td><code>s</code></td><td>Second in minute</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#number" style="color: rgb(202, 0, 0); text-decoration: none; ">Number</a></td><td><code>55</code></td></tr><tr style="background-color: rgb(238, 238, 255); "><td><code>S</code></td><td>Millisecond</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#number" style="color: rgb(202, 0, 0); text-decoration: none; ">Number</a></td><td><code>978</code></td></tr><tr><td><code>z</code></td><td>Time zone</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#timezone" style="color: rgb(202, 0, 0); text-decoration: none; ">General time zone</a></td><td><code>Pacific Standard Time</code>; <code>PST</code>; <code>GMT-08:00</code></td></tr><tr style="background-color: rgb(238, 238, 255); "><td><code>Z</code></td><td>Time zone</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#rfc822timezone" style="color: rgb(202, 0, 0); text-decoration: none; ">RFC 822 time zone</a></td><td><code>-0800</code></td></tr><tr><td><code>X</code></td><td>Time zone</td><td><a target=_blank target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html?noframes=true#iso8601timezone" style="color: rgb(202, 0, 0); text-decoration: none; ">ISO 8601 time zone</a></td><td><code>-08</code>; <code>-0800</code>; <code>-08:00</code></td></tr></tbody></table><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "></p>
package demo; import java.util.Date; import java.text.SimpleDateFormat; import java.text.DateFormat; import java.text.ParseException; import java.util.Calendar; public class Test { public Test() { } public static void main(String[] args) { // 字符串转换日期格式 // DateFormat fmtDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 接收传入参数 // String strDate = args[1]; // 得到日期格式对象 // Date date = fmtDateTime.parse(strDate); // 完整显示今天日期时间 String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new Date()); System.out.println(str); // 创建 Calendar 对象 Calendar calendar = Calendar.getInstance(); try { // 对 calendar 设置时间的方法 // 设置传入的时间格式 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d H:m:s"); // 指定一个日期 Date date = dateFormat.parse("2013-6-1 13:24:16"); // 对 calendar 设置为 date 所定的日期 calendar.setTime(date); // 按特定格式显示刚设置的时间 str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime()); System.out.println(str); } catch (ParseException e) { e.printStackTrace(); } // 或者另一種設置 calendar 方式 // 分別爲 year, month, date, hourOfDay, minute, second calendar = Calendar.getInstance(); calendar.set(2013, 1, 2, 17, 35, 44); str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime()); System.out.println(str); // Calendar 取得当前时间的方法 // 初始化 (重置) Calendar 对象 calendar = Calendar.getInstance(); // 或者用 Date 来初始化 Calendar 对象 calendar.setTime(new Date()); // setTime 类似上面一行 // Date date = new Date(); // calendar.setTime(date); str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime()); System.out.println(str); // 显示年份 int year = calendar.get(Calendar.YEAR); System.out.println("year is = " + String.valueOf(year)); // 显示月份 (从0开始, 实际显示要加一) int month = calendar.get(Calendar.MONTH); System.out.println("nth is = " + (month + 1)); // 本周几 int week = calendar.get(Calendar.DAY_OF_WEEK); System.out.println("week is = " + week); // 今年的第 N 天 int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR); System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR); // 本月第 N 天 int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH); System.out.println("DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH)); // 3小时以后 calendar.add(Calendar.HOUR_OF_DAY, 3); int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY); System.out.println("HOUR_OF_DAY + 3 = " + HOUR_OF_DAY); // 当前分钟数 int MINUTE = calendar.get(Calendar.MINUTE); System.out.println("MINUTE = " + MINUTE); // 15 分钟以后 calendar.add(Calendar.MINUTE, 15); MINUTE = calendar.get(Calendar.MINUTE); System.out.println("MINUTE + 15 = " + MINUTE); // 30分钟前 calendar.add(Calendar.MINUTE, -30); MINUTE = calendar.get(Calendar.MINUTE); System.out.println("MINUTE - 30 = " + MINUTE); // 格式化显示 str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime()); System.out.println(str); // 重置 Calendar 显示当前时间 calendar.setTime(new Date()); str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime()); System.out.println(str); // 创建一个 Calendar 用于比较时间 Calendar calendarNew = Calendar.getInstance(); // 设定为 5 小时以前,后者大,显示 -1 calendarNew.add(Calendar.HOUR, -5); System.out.println("时间比较:" + calendarNew.compareTo(calendar)); // 设定7小时以后,前者大,显示 1 calendarNew.add(Calendar.HOUR, +7); System.out.println("时间比较:" + calendarNew.compareTo(calendar)); // 退回 2 小时,时间相同,显示 0 calendarNew.add(Calendar.HOUR, -2); System.out.println("时间比较:" + calendarNew.compareTo(calendar)); } }
1 0
- Calendar 日历类的基本用法
- Java Calendar 日历类的基本使用
- 日历类Calendar类的用法
- java的Calendar(日历)类
- java的Calendar(日历)类
- Calendar 日历类的使用方法
- Calendar类的日历小程序
- java的Calendar(日历)类 (转)
- Java中日历类Calendar的使用
- Calendar日历的使用
- Calendar日历类和GregorianCalendar公历类用法的一个小结
- Calendar日历类和GregorianCalendar公历类用法的一个小结
- Calendar类的用法
- Calendar类的用法
- 日历类Calendar
- Java日历类Calendar
- Calendar 日历类
- Calendar类日历
- virtualenv
- 电子证据 利用Kali进行wifi钓鱼实战详细教程
- 关于ios8以上原生推送不执行返回deviceToken方法的解决
- Nginx反向代理
- mybatis进行insert操作,得到自增的id
- Calendar 日历类的基本用法
- Linux下的.configure有什么作用
- Demo 基于 Retrofit_RxJava, 并且采用了 MVP 模式
- Unity—Android通讯
- Android常见问题总结(四)
- redhat中执行临时权限设置
- POJ 2773 Happy 2006 (二分+容斥)
- iOS 每次登录进入首页
- 无法解析的外部命令