1 / 12

Java 日期時間函數

Java 日期時間函數. DateTime Method. 日期時間類別庫. java.util .* java.util.Calendar java.util.GregorianCalendar java.util.TimeZone java.util.SimpleTimeZone java.util.Date java.text .* java.text.DateFormat java.text.SimpleDateFormat java.text.DateFormatSymbols. java.util.Date. 宣告物件

sarai
Télécharger la présentation

Java 日期時間函數

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java 日期時間函數 DateTime Method

  2. 日期時間類別庫 • java.util.* • java.util.Calendar • java.util.GregorianCalendar • java.util.TimeZone • java.util.SimpleTimeZone • java.util.Date • java.text.* • java.text.DateFormat • java.text.SimpleDateFormat • java.text.DateFormatSymbols

  3. java.util.Date • 宣告物件 Date today = new Date(); • 方法 • 抓取時getHours() • 抓取分 getMinutes() • 抓取秒 getSeconds()

  4. 範例 import java.util.Date; public class TimeExample1{ public static void main(String args[]) { Date nowTime = new Date(); System.out.println(nowTime.getHours()+":“ +nowTime.getMinutes()+":“ +nowTime.getSeconds()); } }

  5. java.util.Calendar • Calendar 日曆類別 • 參考網址http://nothing.tw/JDK_API_1_6/java/util/Calendar.html • 宣告物件取得現在的時間 Calendar today = Calendar.getInstance();

  6. 範例 import java.util.Calendar; public class CalendarExample1{ public static void main(String args[]) { Calendar today = Calendar.getInstance(); System.out.println("現在時區ERA:"+today.get(Calendar.ERA)); System.out.println("現在年份:"+today.get(Calendar.YEAR)); System.out.println("今天日期:"+today.get(Calendar.DATE)); System.out.println("今天是這個月的幾號:"+today.get(Calendar.DAY_OF_MONTH)); System.out.println("今天是這星期的第幾天:"+today.get(Calendar.DAY_OF_WEEK)); System.out.println("現在幾點:"+today.get(Calendar.HOUR)); } }

  7. java.text.DateFormat • DateFormat是日期/時間格式化子類別的抽象類別。(設定日期時間格式用。) • 參考網址 http://nothing.tw/JDK_API_1_6/java/text/DateFormat.html • 方法 • getInstance() • getDateInstance() • getTimeInstance() • getDateTimeInstance() • DateFormat.格式化樣式 • FULL • LONG • MEDIUM • SHORT

  8. 範例 import java.text.DateFormat; import java.util.Date; public class DateFormatExample1 { public static void main(String[] args) { Date now = new Date(); System.out.println(" 1. " + now.toString()); System.out.println(" 2. " + DateFormat.getInstance().format(now)); System.out.println(" 3. " + DateFormat.getTimeInstance().format(now)); System.out.println(" 4. " + DateFormat.getDateTimeInstance().format(now)); System.out.println(" 5. " + DateFormat.getTimeInstance(DateFormat.SHORT).format(now)); System.out.println(" 6. " + DateFormat.getTimeInstance(DateFormat.MEDIUM).format(now)); System.out.println(" 7. " + DateFormat.getTimeInstance(DateFormat.LONG).format(now)); //續下頁

  9. //承上頁 System.out.println(" 8. " + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(now)); System.out.println(" 9. " + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(now)); System.out.println("10. " + DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(now)); System.out.println("11. " + DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(now)); } }

  10. SimpleDateFormat import java.text.SimpleDateFormat; import java.util.Date; public class SimpleExample1{ public static void main(String args[]) { Date today = new Date(); SimpleDateFormat f1=new SimpleDateFormat("yyyy/M/d a h:m"); SimpleDateFormat f2=new SimpleDateFormat("yyyy/MMM/d H:m:s"); System.out.println(f1.format(today)); System.out.println(f2.format(today)); } }

  11. java.text.SimpleDateFormat

  12. java.text.SimpleDateFormat

More Related