Determine if the date format meets the requirements
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
public class Test2
{
public static void Main (string[] args)
{
String date_string= "201609";
Use the SimpleDateFormat class in Java, specify the date format, and note the YYYY,MM case
Here the date format requirements Javaapi in the detailed description, if not clear can download the relevant API to view
SimpleDateFormat format=new SimpleDateFormat ("yyyy-mm");
SimpleDateFormat format=new SimpleDateFormat ("Yyyymm");
Set Date conversion Success ID
Boolean dateflag=true;
Here to catch the exception information
Try
{
Date date = Format.parse (date_string);
} catch (ParseException e)
{
Dateflag=false;
}finally{
Success: TRUE; failure: false
SYSTEM.OUT.PRINTLN ("Date meets Requirements" +dateflag);
}
}
}
Here are the requirements for the date format in the Java API.
| Letters |
date or time element |
represents |
Example |
G |
Era Marker |
Text |
AD |
y |
Years |
Year |
1996;96 |
M |
Month of the Year |
Month |
July; Jul;07 |
w |
Number of weeks in the year |
Number |
27 |
W |
Number of weeks in the month |
Number |
2 |
D |
Number of days in the year |
Number |
189 |
d |
Number of days in month |
Number |
10 |
F |
Week of the Month |
Number |
2 |
E |
Days of the Week |
Text |
Tuesday;Tue |
a |
AM/PM Mark |
Text |
PM |
H |
Number of hours in the day (0-23) |
Number |
0 |
k |
Number of hours in the day (1-24) |
Number |
24 |
K |
Number of hours in am/pm (0-11) |
Number |
0 |
h |
Number of hours in am/pm (1-12) |
Number |
12 |
m |
Number of minutes in hours |
Number |
30 |
s |
Number of seconds in minutes |
Number |
55 |
S |
Number of milliseconds |
Number |
978 |
z |
Time |
General Time Zone |
Pacific Standard Time; PST;GMT-08:00 |
Z |
Time |
RFC 822 Time Zone |
-0800 |
The following example shows how to interpret the date and time patterns in the U.S. locale. Given the date and time of the United States Pacific Time zone for local time 2001-07-04 12:08:56.
| Date and Time mode |
Results |
"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" |
12 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 |
"yyMMddHHmmssZ" |
010704120856-0700 |
"yyyy-MM-dd‘T‘HH:mm:ss.SSSZ" |
2001-07-04T12:08:56.235-0700 |
How to determine whether a date string is the specified format in Java