The first, Refer to table below which defines some patterns:
Letter | Description | Examples |
y | Year | 2013 |
M | Month in year | July, 07, 7 |
d | Day in month | 1-31 |
E | Day name in week | Friday, Sunday |
a | Am/pm marker | AM, PM |
H | Hour in day | 0-23 |
h | Hour in am/pm | 1-12 |
m | Minute in hour | 0-60 |
s | Second in minute | 0-60 |
1. String date = "2016-01-10"
public class TestDateExample2\ {
public static void main(String[] argv) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateInString = "2016-01-10";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
2. String date = "8-Jun-2016"
public class TestDateExample {
public static void main(String[] argv) {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "8-Jun-2016";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
3. String date = "Fri, Jun 8 2016"
public class TestDateExample {
public static void main(String[] argv) {
SimpleDateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy");
String dateInString = "Fri, Jun 8 2016";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
4. String date = "Monday, Jun 8, 2016 10:30:00 AM"
public class TestDateExample {
public static void main(String[] argv) {
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
String dateInString = "Monday, Jun 8, 2016 10:30:00 AM";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Good luck!
0 comments:
Post a Comment