A guide to Java SimpleDateFormat in examples
来源:互联网 发布:小米服务号是什么软件 编辑:程序博客网 时间:2024/05/17 08:44
http://www.javablogging.com/java-simpledateformat-examples/
Today I want to show you some examples of how you can use SimpleDateFormat class in your code. I hope some of them will be new and surprising!
The basic example
First the most basic usage of the class. Lets use it to format Date object into a simple string showing day, month and a year:
1:2:3:4:5:6:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy");String dateAsString = simpleDateFormat.format(new Date());System.out.println(dateAsString);// Result: "12/11/09"
Slightly more advanced
Obviously we can use more complicated patterns than that (see SimpleDateFormat JavaDoc for the full list). Besides adding more data to be shown we can also add some regular text – to do that in a pattern you need to escape the text with an apostrophe. So here is a fancy way of printing the time and date:
1:2:3:4:5:6:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm 'o''clock on' MMMM dd yyyy");String dateAsString = simpleDateFormat.format(new Date());System.out.println(dateAsString);// Result: "06:49 o'clock on November 12 2009"
Now with Locale
SimpleDateFormat is Locale dependent, so by providing one you can get the Date string localized for specific language or country. This is a date in French:
1:2:3:4:5:6:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMMM yyyy zzzz G", Locale.FRENCH);String dateAsString = simpleDateFormat.format(new Date());System.out.println(dateAsString);// Result: "12 novembre 2009 Heure du méridien de Greenwich ap. J.-C."
Parse String to Date
Good thing about the SimpleDateFormat class is that it can be used not only for formatting, but also for parsing string into a Date object. In this example the used pattern is exactly the same as used byDate.toString() method. With it we can parse the strings created by Data.toString() back into dates:
1:2:3:4:5:6:7:8:9:10:11:12:
// SimpleDateFormat that works exactly like Date.toString()SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy", Locale.ROOT);Date today = new Date();String dateAsString_format = simpleDateFormat.format(today);String dateAsString_native = today.toString();// Both strings are: "Thu Nov 12 18:49:36 GMT 2009"Date parsedDate = simpleDateFormat.parse(dateAsString_native);System.out.println(parsedDate.toString());// Result: "Thu Nov 12 18:49:36 GMT 2009"
Setting the default time zone
Notice that when you parse a string into a Date the result may be ambiguous. For example the string“11:23 1 Jan 2001″ is a different moment in time depending whether you live in Japan or Canada. By default Java resolves this by using your local time zone obtained from the default Locale.
Since the time zone may not be a part of the parsed string you may want to set it manually instead using the default one. You can do this withsetTimeZone() method:
1:2:3:4:5:6:7:8:9:10:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);Date date = simpleDateFormat.parse("13-07-1999");System.out.println(date);// Result: "Tue Jul 13 00:00:00 GMT 1999"simpleDateFormat.setTimeZone(TimeZone.getTimeZone("PST"));date = simpleDateFormat.parse("13-07-1999");System.out.println(date);// Result: "Tue Jul 13 08:00:00 GMT 1999"
Setting the century
Similar ambiguity occurs when parsing two-digit year dates. Whether “01/01/59″ is the new year’s day of 1959 or 2059 can be set by specifying the century to the SimpleDateFormat object. To be specific withset2DigitYearStart() method you can specify the 100 year period in which the parsed date will be placed. By default the 100 year period is[today - 80 years, today + 20 years]. See the example:
1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMMM yy", Locale.ENGLISH);Date date = simpleDateFormat.parse("1 November 45");System.out.println(date);// By default the year 45 will be taken from period 1929 - 2029// Result: "Thu Nov 01 00:00:00 GMT 1945"Date startOf19thCentury = new GregorianCalendar(1801,1,1).getTime();Date startOf21stCentury = new GregorianCalendar(2001,1,1).getTime();simpleDateFormat.set2DigitYearStart(startOf19thCentury);date = simpleDateFormat.parse("1 November 45");System.out.println(date);// After invoking set2DigitYearStart() the period is now 1801 - 1901// Result: "Thu Nov 01 00:00:00 GMT 1845"simpleDateFormat.set2DigitYearStart(startOf21stCentury);date = simpleDateFormat.parse("1 November 45");System.out.println(date);// This time the period is 2001 - 2101// Result: "Thu Nov 01 00:00:00 GMT 2045"
Make the parsing more strict
By default the parse method of SimpleDateFormat is very forgiving. If the provided string is not entirely compatible with the pattern, SimpleDateFormat instead of giving up tries with many tricky heuristics to guess what would be the correct answer. In many situations this behavior is more than welcome, but if you do not like this you can disable it. WithsetLenient() method you can make the parsing obey exactly the pattern and throw an exception if string is invalid.
1:2:3:4:5:6:7:8:9:10:11:12:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH);Date date = simpleDateFormat.parse("31 April 1999");System.out.println(date);// Since April has only 30 days the parser will assume// user meant April 30th + one day = May 1st// Result: "Sat May 01 00:00:00 GMT 1999"// Make the parsing more strictsimpleDateFormat.setLenient(false);// This throws java.text.ParseException: Unparseable date: "31 April 1999"date = simpleDateFormat.parse("31 April 1999");
- A guide to Java SimpleDateFormat in examples
- A guide to Java SimpleDateFormat in examples
- A guide to object cloning in java
- Java Examples in a Nutshell
- A Complete Guide to Programming in C++
- A Guide to Branching in Mercurial
- A Programmers Guide to ADO.NET in C#
- A field guide to Zones in OpenSolaris 2008.05
- Persistence in the Enterprise: A Guide to Persistence Technologies
- A GENTLE GUIDE TO USING BATCH NORMALIZATION IN TENSORFLOW
- 7 Strace Examples to Debug the Execution of a Program in Linux
- 7 Strace Examples to Debug the Execution of a Program in Linux
- A Guide to Kill, Pkill and Killall Commands to Terminate a Process in Linux
- Eclipse in Action: A Guide for the Java Developer
- A Visual Guide to Swing Components (Java Look and Feel)
- Java Q&A : SimpleDateFormat 格式
- Apache Tutorial: Enable mod_rewrite In Windows A brief action guide to enable mod_rewrite in Windows
- linux_A Guide to Kill, Pkill and Killall Commands to Terminate a Process in Linu
- MSSQL发布订阅中解决含有Text域的数据发布问题
- GOF 设计模式
- 几何摘录
- hdu 2588 搞了好久的数论题 1到n的数与n的公约数大于m的数的个数
- android2.2源码在eclipse中使用
- A guide to Java SimpleDateFormat in examples
- Oracle创建定时器
- test
- Android OTA 升级之一:编译升级包
- 模式比较
- SVN使用规范
- 研究生生活怎样度过
- 堆排序(上)
- 2012第十届Esri用户大会掌上参会指南下载地址