new Date() to Julian date format in java

前端 未结 3 1733
余生分开走
余生分开走 2021-01-29 16:00

I need to convert new Date() to Julian date format.is there is any build in java function for this. my exact requirement is

Represents the creation date of the file in

相关标签:
3条回答
  • 2021-01-29 16:06

    Actually I think what you need is

    String yearYy = new SimpleDateFormat("yy").format(today)
    String dayD = new SimpleDateFormat("D").format(today)
    String dayDDD = dayD.padLeft(3,'0')
    String julianDateString = yearYy + dayDDD
    

    This gives the proper Julian date format - there shouldn't be a leading '0', but you do need to pad the day number so that it's always 3 characters.

    ...I'm quite sure this could be simplified, but the important thing is that the day number should be padded.

    So 20/01/21 gives 21020 (rather than 02120 when using the previous example)

    0 讨论(0)
  • 2021-01-29 16:22

    java.time

    I recommend that you use java.time, the modern Java date and time API, for your date work. The format you need is built in.

        LocalDate today = LocalDate.now(ZoneId.systemDefault());
        String ordinalDateString = today.format(DateTimeFormatter.ISO_ORDINAL_DATE);
        System.out.println(ordinalDateString);
    

    Output for today January 20, 2021 in standard ISO 8601 format:

    2021-020

    The format you mention, 0YYDDD, is peculiar. It’s nothing I have seen before. If you’re serious about it, define a formatter that gives it:

        DateTimeFormatter peculiarDateFormatter = DateTimeFormatter.ofPattern("0uuDDD");
    

    021020

    Ordinal date, not Julian date

    The day number of the year that you ask for is called the ordinal date, which is why the built-in formatter also has ordinal in its name. A Julian day is something else, the continuous count of days since January 1, 4713 BCE. The ordinal date is sometimes referred to as Julian, but there is nothing genuinely Julian about it, so to avoid confusion, prefer ordinal over Julian.

    Links

    • Oracle tutorial: Date Time explaining how to use java.time.
    • Julian day on Wikipedia.
    • Ordinal date on Wikipedia.
    0 讨论(0)
  • 2021-01-29 16:28

    Use SimpleDateFormat.

    The following code returns the Julian date string for date according to the format you gave.

    String julianDateString = new SimpleDateFormat("'0'yyD").format(date);
    
    0 讨论(0)
提交回复
热议问题