snippetjavaMinor
Date Format provider
Viewed 0 times
formatproviderdate
Problem
I am facing a issue with an API to handle dates in Java. I am not a very experienced programmer and my English is very bad.
Problem
I'm working in a large project with many subsystems, and I want to make a Date Utility library to manage all dates in the system.
Requirements
Platform
Business Logic
The long format is for details, and the short is for grids and places with little space.
API
Parser and Formatter
```
protected synchronized String dateToString(Date date, String format) {
if (date == null) {
return EMPTY_STRING;
}
return getForma
Problem
I'm working in a large project with many subsystems, and I want to make a Date Utility library to manage all dates in the system.
Requirements
Platform
- The front end is a JSF page with
RichFaces
- The application generate reports with
JasperReports
- The back end is a Spring 3.1 Application
- Is desirable to support
CalendarandXMLGregorianCalendar(for Jaxb2 Serialization)
Business Logic
- The system must support six types of dates:
- Short date format: dd-MM-yy
- Date format: dd-MM-yyyy
- Time format (only hours and minutes): HH:mm
- Time and date: dd-MM-yyyy HH:mm
- Time and short date: dd-MM-yy HH:mm
- The consistency is the most important thing in the design
The long format is for details, and the short is for grids and places with little space.
API
// Format using dd-MM-yy
public String asShortDate(@Nullable Date date) ;
// Parse using dd-MM-yyy
public Date parseShortDate(@Nullable String string) throws ParseException;
// Format using dd-MM-yyyy
public String asDate(@Nullable Date date);
// Parse using dd-MM-yyyy
public Date parseDate(@Nullable String string) throws ParseException;
// Format using HH:mm
public String asTime(@Nullable Date date);
// Parse using HH:mm
public Date parseTime(@Nullable String string) throws ParseException;
// Format using dd-MM-yyyy HH:mm
public String asDateTime(@Nullable Date date);
// Parse using dd-MM-yyyy HH:mm
public Date parseDateTime(@Nullable String string) throws ParseException;
// Format using dd-MM-yy HH:mm
public String asShortDateTime(@Nullable Date date) ;
// Parse using dd-MM-yy HH:mm
public Date parseShortDateTime(@Nullable String string) throws ParseException;Parser and Formatter
```
protected synchronized String dateToString(Date date, String format) {
if (date == null) {
return EMPTY_STRING;
}
return getForma
Solution
Dates and Date handling in Java have been a problem since the beginning (check out all the deprecated methods in version 1.1). You appear to be running in to the same problems that may Java developers have encountered, and then there are a whole lot of issues that you have not yet encountered.
Unlike other questions in CodeReview, I'm going to deviate from the norm, and tell you to give up.... A lot of people have taken a kick at this can... people smarter than you, and me, and the bottom line is that, even after three attempts, the base Java implementation for Date and Date-like data is still not right.
On the other hand, JodaTime has somehow managed to be a library that is well maintained, accurate, usable, and just 'gets it'. It is licensed under the Apache2 license, which means there is effectively no restriction on how you can use it, and with few restrictions/requirements when modifying it (not that you would want to).
At this point in the Java ecosystem, there is absolutely no point in building your own Date library.... it leads to weeping and gnashing of teeth.
Unlike other questions in CodeReview, I'm going to deviate from the norm, and tell you to give up.... A lot of people have taken a kick at this can... people smarter than you, and me, and the bottom line is that, even after three attempts, the base Java implementation for Date and Date-like data is still not right.
On the other hand, JodaTime has somehow managed to be a library that is well maintained, accurate, usable, and just 'gets it'. It is licensed under the Apache2 license, which means there is effectively no restriction on how you can use it, and with few restrictions/requirements when modifying it (not that you would want to).
At this point in the Java ecosystem, there is absolutely no point in building your own Date library.... it leads to weeping and gnashing of teeth.
Context
StackExchange Code Review Q#31268, answer score: 8
Revisions (0)
No revisions yet.