Here i m discuss SimpleDateFormat class SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> String), parsing (String -> date). It allows you to choosing any user-defined date-time formatting patterns.
Letter | Date or Time Component | Presentation | Examples |
---|---|---|---|
G | Era designator | Text | AD |
y | Year | Year | 1996 ; 96 |
M | Month in year | Month | July ; Jul ; 07 |
w | Week in year | Number | 27 |
W | Week in month | Number | 2 |
D | Day in year | Number | 189 |
d | Day in month | Number | 10 |
F | Day of week in month | Number | 2 |
E | Day in week | Text | Tuesday ; Tue |
a | Am/pm marker | Text | PM |
H | Hour in day (0-23) | Number | 0 |
k | Hour in day (1-24) | Number | 24 |
K | Hour in am/pm (0-11) | Number | 0 |
h | Hour in am/pm (1-12) | Number | 12 |
m | Minute in hour | Number | 30 |
s | Second in minute | Number | 55 |
S | Millisecond | Number | 978 |
z | Time zone | General time zone | Pacific Standard Time ; PST ; GMT-08:00 |
Z | Time zone | RFC 822 time zone | -0800 |
refer this link know to more...
public class SimpleDateFormat extends DateFormat
SimpleDateFormatPattern .Java Sample Code
public static void main(String[] args) throws ParseException { SimpleDateFormat format = null; String startDate = "2013-01-08 12:10:56"; String endDate = "2013-12-09 10:10:56"; System.out.println("<<<<<<<<<<<<<<--------Compare 2 Dates------------>>>>>>>>>>>>>>>>>>>>"); new SimpleDateFormatPattern().compareTwoDate( format , startDate , endDate); System.out.println("<<<<<<<<<<<<<<--------Convert String into Date------->>>>>>>>>>>>>>>>>"); new SimpleDateFormatPattern().convertStringToDate(format , startDate , endDate); System.out.println("<<<<<<<<<<<<<<--------Convert Date into String------>>>>>>>>>>>>>>>>>>"); new SimpleDateFormatPattern().convertDateToString(format); }Compare 2 Dates
private void compareTwoDate(SimpleDateFormat format, String startDate, String endDate) { Date date1 , date2; format = new SimpleDateFormat("yyyy-MM-dd"); try { date1 = format.parse(startDate); date2 = format.parse(endDate); if (date1.compareTo(date2) < 0) System.out.println("Start Date is before than End Date"); else if(date1.compareTo(date2) >0) System.out.println("Start Date is after than End Date"); else System.out.println("Start Date and End Date both are equal"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } }Convert String into Date
private void convertStringToDate(SimpleDateFormat format, String startDate, String endDate) { format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); try { Date startDates = format.parse(startDate); Date endDates = format.parse(endDate); System.out.println("String to date :"+startDates); System.out.println("String to date :"+endDates); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } }Convert Date into String
private void convertDateToString(SimpleDateFormat format) { format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(); String todayDate = format.format(date); System.out.println("Date to String :" +todayDate); }
after executing SimpleDateFormatPattern.java following result shown :-
No comments:
Post a Comment