tl;dr
LocalDate.parse(
"16-Mar-2017" ,
DateTimeFormatter.ofPattern( "dd-MMM-uuuu" , Locale.US )
)
java.time
The modern approach is with java.time classes. Avoid the notoriously troublesome old date-time classes, now legacy.
Parse date string
Parse the date string by defining a formatting pattern to match. Specify a Locale
for human language to use in translating the name of the month.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MMM-uuuu" , Locale.US );
String input = "16-Mar-2017" ;
The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate localDate = LocalDate.parse( input , f );
JD Edwards EnterpriseOne date format
If by JDE
you meant “JD Edwards EnterpriseOne”, those systems use an unusual format for representing a date-only value as a string, CYYDDD
where:
C
= Century, 0 for 1900s, 1 for 2000s. Add to 19
to get century number, multiply by 100
to get year number.
YY
= Year of the century.
DDD
= Day of year, running 1 to 365 (or 366 in Leap Year).
Let's build up a String in that format.
// Build output in CYYDDD format used by JD Edwards EnterpriseOne.
int c = ( ( localDate.getYear ( ) / 100 ) - 19 );
String yy = ( String.valueOf ( localDate.getYear ( ) ) ).substring ( 2 ); // Substring of positions 3-4, index numbering is 2.
String ddd = String.format ( "%03d", localDate.getDayOfYear ( ) );
String output = c + yy + ddd ;
Dump to console.
System.out.println ("input: " + input );
System.out.println ( "output: " + output );
When run.
input: 16-Mar-2017
output: 117075
Now go the other direction, parsing a JDE date string to get a LocalDate
. We extract the century code of 1
, add it to 19
, and multiply by a hundred, and lastly add the two digits for year-of-century. From that integer year number we create a Year
object. By feeding that Year
object the parsed integer number for day-of-year, we get a LocalDate
object.
// Going the other direction, parsing CYYDDD to get a `LocalDate`.
String cyyddd = "117075";
String c_ = cyyddd.substring ( 0, 0 + 1 ); // Index-counting, zero-based.
String yy_ = cyyddd.substring ( 1, 1 + 2 );
String ddd_ = cyyddd.substring ( 3 );
Year year = Year.of ( ( ( Integer.valueOf ( c_ ) + 19 ) * 100 ) + Integer.valueOf ( yy_ ) );
LocalDate ld = year.atDay( Integer.valueOf ( ddd_ ));
Dump to console.
System.out.println ("cyyddd: " + cyyddd );
System.out.println ("ld: " + ld );
cyyddd: 117075
ld: 2017-03-16
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.