- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeTest.java
More file actions
Latest commit
37 lines (34 loc) · 1.26 KB
/
Copy pathTimeTest.java
File metadata and controls
37 lines (34 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
importjava.text.DateFormat;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Date;
importjava.util.Locale;
importjava.util.TimeZone;
classTimeTest{
publicstaticDateparseShortDate(StringdateTime) {
try {
SimpleDateFormatformatter = newSimpleDateFormat("MM/dd/yy", Locale.getDefault());
formatter.setTimeZone(TimeZone.getDefault());
returnformatter.parse(dateTime);
} catch (ParseExceptionignored) {}
returnnull;
}
/**
* Formats timestamp to 'date month' format (e.g. '3 Feb 2019'). //"dd-MM-yyyy"
*/
publicstaticStringformatShortDate(StringdateTime) {
SimpleDateFormatdateFormat = newSimpleDateFormat("dd MMM yyyy", Locale.getDefault());
dateFormat.setTimeZone(TimeZone.getDefault());
Datevalue = parseShortDate(dateTime);
if (value == null){
returnnull;
}
returndateFormat.format(value);
}
publicstaticvoidmain(Stringargs[]){
System.out.println("Dare: "+ formatShortDate("30/12/2020"));
System.out.println("Dare: "+ formatShortDate("12/30/2020"));
System.out.println("Dare: "+ formatShortDate("12/29/2020"));
}
}