-2

I want to convert dd-MON-yyyy to Julian date as part of SQL query against JDE. It would be of great help if anyone could guide on how to achieve this.

new JulianDate().ConvertToJulian(date1) worked when date1 was in mm/dd/yyyy format. But when date1 is in dd-MON-yyyy format i get the error:

java.text.ParseException: Unparseable date: "16-Mar-2017"

In JDE, the date is stored in Julian Format.

Please note here mm/dd/yyyy and dd-MON-yyyy are all in string format.

Hence DateFormat, SimpleDateFormat, etc cannot be applied.

Also in SQL i believe it is dd-MON-yyyy format and not dd-MMM-yyyy format.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
user7324003
  • 49
  • 1
  • 9

3 Answers3

2

You need to use the dd-MMM-yyyy format for this.

Take a look at the code below. This converts given date format to Julian format.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatConverter {

    private String inputDateFormat;
    private String outputDateFormat;

    private DateFormatConverter (String inputDateFormat, String outputDateFormat) {
        this.inputDateFormat = inputDateFormat;
        this.outputDateFormat = outputDateFormat;
    }

    private String convert(String inputDate) throws ParseException {
        SimpleDateFormat idf = new SimpleDateFormat(inputDateFormat);
        SimpleDateFormat odf = new SimpleDateFormat(outputDateFormat);
        Date date = idf.parse(inputDate);
        String outputDate = odf.format(date);
        return outputDate;
    }

    public static String toJulian(String inputFormat, String inputDate) throws ParseException {
        String suffixFormat = "yyDDD";
        String prefixFormat = "yyyy";
        String suffix = new DateFormatConverter(inputFormat, suffixFormat).convert(inputDate);
        int centuryPrefix = Integer.parseInt(new DateFormatConverter(inputFormat, prefixFormat).convert(inputDate).substring(0, 2))-19;
        return centuryPrefix+suffix;
    }

    public static void main(String[] args) throws ParseException {
        String jd = DateFormatConverter.toJulian("dd-MMM-yyyy", "01-Jan-2017");
        System.out.println(jd);
    }

}

Extra:

Refer to this info about Julian dates:

https://docs.oracle.com/cd/E26228_01/doc.93/e21961/julian_date_conv.htm#WEAWX259


Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31
  • thank you for the detailed code. I want to convert the date to Julian format. – user7324003 Mar 16 '17 at 09:16
  • Thank you so much. I converted to Julian format after carrying out the conversion as you said. – user7324003 Mar 16 '17 at 09:32
  • i am new to date formatting and all the queries above were confusing me. Thank you for the help. @anacron – user7324003 Mar 16 '17 at 09:34
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Mar 18 '17 at 02:40
  • @BasilBourque. That's true. We can incorporate those changes too. – anacron Mar 18 '17 at 03:18
1

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?

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

I did the following:

String date1="dd-MMM-yyyy";
String date2="MM/dd/yyyy";
SimpleDateFormat idf = new SimpleDateFormat(date1);
SimpleDateFormat odf = new SimpleDateFormat(date2);
Date dateFrom = idf.parse(fromDate);
Date dateTo = idf.parse(toDate);

And then to convert to Julian Date i used:

new JulianDate().ConvertToJulian(odf.format(dateFrom));
new JulianDate().ConvertToJulian(odf.format(dateTo));

Hoping somebody would find it useful in future.

user7324003
  • 49
  • 1
  • 9