Presumably everyone is not unfamiliar with SimpleDateFormat. SimpleDateFormat is a very common class in Java that is used to parse and format date strings, but it can cause very subtle and difficult-to-debug problems if used accidentally, because DateFormat and SimpleDateFormat Classes are not thread-safe, calling the format () and Parse () methods in a multithre
, the following rules apply.
Month: If the number of pattern letters is 3 or greater than 3, the month is interpreted as text;
For formatting, if the number of pattern letters is 2, the year is truncated to 2 digits, otherwise the year is interpreted to number.
For parsing, if the number of pattern letters is greater than 2, the year is interpreted literally, regardless of the number of digits. So use the pattern "mm/dd/yyyy" to parse "01/11/12" to January 11, 12 A.D.
When parsing the abbreviate
public class
SimpleDateFormat extends DateFormat
SimpleDateFormatis a specific class that formats and analyzes data in a country-sensitive manner. It allows formatting (date-and-text), parsing (text, date), and normalization. SimpleDateFormat allows you to start by selecting any user-specified method for date-time formatting. However, want to use dateformat gettimeinstance , getdatei
-and text), parsing (text-to-date), and normalization. SimpleDateFormat allows you to select any user-defined pattern of date-time formats. However, it is still recommended to pass the gettimeinstance,getdateinstance , or dateformat in the getdatetimeinstance to create a date-time formatter. Each of these class methods can return a date/time formatter initialized in the default format pattern. You can use the applypattern method to modify the for
));
}
The output is as follows:
SimpleDateFormat API Reference First ref, date format Template example is as follows:
5. How to Obtain Date Object 1 by string class strdate = Date.tostring () such as ("Mon Apr 10:32:03 CST 2018") method1 (deprecated): date Class is constructed by date = new Date (strdate)
Examples are as follows:
public static void Main (string[] args) throws ParseException {
Date
, we usually use a singleton or static variable to modify it, which is generally the case:ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date; Public classDateutil {Private StaticSimpleDateFormat SDF =NewSimpleDateFormat ("Yyyymm"); /*** Get the number of months *@paramTime 201202 *@return */ Public Static intGetDays (String time)throwsException {//String time = "201202"; Date Date =Sdf.parse (time); Calendar C=calendar.getinstance (); C.settime (date);
so on, are given to Calendar for storage. This can cause a problem if your simpledateformat is static, so multiple thread The SimpleDateFormat is shared and the calendar reference is shared, and there is a time-disordered situation.Workaround(1) The first method is also the simplest solution. We can remove the static so that each new thread will have its own SDF instance to avoid thread-safety problems.Sim
[Preface]
Recently, the project requires a special date format, which exactly belongs to the string type. In the past, simpledateformat was not used to format the date. The idea in my mind is to first give the date to tostring, then cut the string slowly! Oh, my God, there are all unscientific methods. After reading the notes, you can use simpledateformat to process them. Here we will summarize the usag
Label: simpledateformat time format
In Java, how can we convert a date to the desired format, or convert a string to a date in a certain format, such as converting a date or time in the database into a desired format, java provides the simpledateformat class for implementation. The following is the usage and example of simpledateformat:Java. Lang. Object
Java. Te
identifier in the formatted date. [Java]View plain copy
SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month dd Day hh mm min ss sec");
It's written like this. The final result is wrong.
[Java]View plain copy
2013 August 14th 15 o'clock 08 minutes 59 seconds
[Java]View plain copy
This mm is written here is the month of the expression. And the minute i
public classSimpleDateFormatExtends DateFormatSimpleDateFormat is a specific class that formats and analyzes data in an individual, sensitive way. It allows formatting (date-and-text), parsing (text, date), and normalization. SimpleDateFormat allows you to start by selecting any user-specified mode with date-time formatting. However, you want to create a date-time formatter with gettimeinstance, getdateinstance, or getdatetimeinstance in DateFormat. E
Date,dateformat,simpledateformat,calendar 650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "Styl E= "Background:url ("/e/u261/lang/zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= " Spacer.gif "/>1.Date class:Under the Java.util package, the class Date represents a specific moment, accurate to milliseconds. starting with JDK 1.1, you should use the Calendar class to convert between date and Time fields,
simpledateformat will be destroyed in high concurrency, which is extremely resource-consuming.
2. Synchronized
Synchronize simpledateformat objects with synchronized.
Problems:
When the object is used for high concurrency, blocking occurs. When the current user is used, other users wait. Although the result is correct, the concurrency becomes a queue, which does not solve the problem, it also affects per
Java uses the SimpleDateFormat class's constructor, SimpleDateFormat (String str), to construct a formatted date format.Formats the specified Date object as a string in the specified format through the format (date date) method.I wrote an example myself:@Test Public voidtestBasicPTFWJG1 () {SimpleDateFormat SDF=NewSimp
Example of a SimpleDateFormat function:SimpleDateFormat format=New SimpleDateFormat ("Mm-dd HH:mm:ss E"); StringTime =format.format (new Date ()); System.out.println ("Current time:" +time);Second, SimpleDateFormat function syntax:G-Age markery yearsM-monthD-Dayh in the morning or afternoon (1~12)H at the time of Day
, which is extremely resource-intensive.2.2, synchronizedSynchronizes SimpleDateFormat objects with synchronized.Problems that exist:When high concurrency occurs, blocking is used for the object, and other users wait while the current consumer is in use, although the result is correct, but the concurrency becomes queued, which in fact does not resolve the problem, and also affects performance and efficiency.2.3, ThreadLocalUse threadlocal to make each
Package Com.day10.SimpleDateFormat;Import Java.text.DateFormat;Import java.text.ParseException;Import Java.text.SimpleDateFormat;Import Java.util.Date;public class Demosimpledateformat {/*** @param args* Common Objects (SimpleDateFormat class implements the conversion of dates and strings) (master)* Overview of the A:dateformat class* DateFormat is an abstract class of date/time formatting subclasses that f
SimpleDateFormat is a specific class that formats and parses dates in a language-related way. It allows formatting (date-and text), parsing (text-to-date), and normalization.SimpleDateFormat allows you to select any user-defined pattern of date-time formats. However, it is still recommended to pass the gettimeinstance,getdateinstance , or getdatetimeinstance in dateformat To create a date-time formatter. Each of these class methods can return a date/t
effect. Also, add some caveats and solutions to the actual Android development: 1:handler This Handler class should be static or leaks might occur:incominghandler@SuppressLint ("Handlerleak")Private Handler Mhandler = new Handler () {@Overridepublic void Handlemessage (msg) { };};Workaround: Private Handler Mhandler = new Handler (new Handler.callback () {@Overridepublic boolean handlemessage (msg) {return false;}}); 2:simpledateformat To the local
Date and Time Pattern
Result
"YYYY. Mm.dd G ' at ' HH:mm:ss z '
2001.07.04 AD at 12:08:56 PDT
"EEE, MMM D, ' yy"
Wed, Jul 4, ' 01
"H:mm a"
12:08 PM
"HH ' o ' clock ' A, zzzz"
O ' clock PM, Pacific Daylight Time
"K:mm A, z"
0:08 PM, PDT
"Yyyyy. MMMMM.DD GGG hh:mm AAA "
02001.july.04 AD 12:08 PM
"EEE, D MMM yyyy HH:mm:ss Z"
Wed, 4 Jul 2001 12:08:56-0700
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.