2

I have a number of day of the year. (Today's number of the day is 308.) I want to convert this number to a date in SQL format (YYYY/MM/DD). Can I do it in java?

Thank you in advance.

jarlh
  • 42,561
  • 8
  • 45
  • 63
Asjon
  • 166
  • 2
  • 12
  • 1
    [SimpleDateFormat](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) for example has a format `D`, which represents the day in the year. You could simply parse it to a `Date` by providing the number – SomeJavaGuy Nov 04 '15 at 10:05
  • 1
    There's no such thing as a SQL format, or a date format at all. Dates are stored as *binary* values in all databases. Formats apply only when you cast the dates from/to strings, or if you store the date as a string instead of an actual date-typed value – Panagiotis Kanavos Nov 04 '15 at 10:06
  • 1
    The ANSI SQL date format is YYYY-MM-DD. But you want to it in Java? – jarlh Nov 04 '15 at 10:06
  • 1
    @Asjon if your database field is date-typed, there's no reason to convert the date to string. Just calculate the current `Date` and store it to the database *as a date*. – Panagiotis Kanavos Nov 04 '15 at 10:07
  • @PanagiotisKanavos, how the date values are stored on disk is unimportant. The SQL standard has a specified date format. (However, some dbms products do things their own way...) – jarlh Nov 04 '15 at 10:07
  • Which version of Java are you using? – Jon Skeet Nov 04 '15 at 10:08
  • 1
    @jarlh no it doesn't. What you are talking about is the ISO 8601 *date literal*, not a date format. Date values should not be passed as strings at all. All data access libraries allow you to pass strongly-typed parameters to parameterized queries – Panagiotis Kanavos Nov 04 '15 at 10:09
  • you can refer this page: http://stackoverflow.com/questions/4048688/how-can-i-convert-day-of-year-to-date-in-javascript – Ivin Raj Nov 04 '15 at 10:09

2 Answers2

6

tl;dr

Year.of( 2018 )
    .atDay( 308 )

2018-11-04

java.time

The modern approach uses the java.time classes built into Java 8 and later.

The LocalDate class represents a date-only value without time-of-day and without time zone.

The Year class represents, well, a year. And includes a atDay( dayOfYear ) method where you pass your ordinal day of the year.

int dayOfYear = 308 ;
Year y = Year.of( 2018 ) ;
LocalDate ld = y.atDay( dayOfYear ) ;

Dump to console.

System.out.println( "dayOfYear: " + dayOfYear + " at year: " + y + " = " + ld ) ;

See this code run live at IdeOne.com.

dayOfYear: 308 at year: 2018 = 2018-11-04

As for generating a string in a particular format, search for DateTimeFormatter class as that has been covered many many times already.

And by the way, your particular format is not at all a “SQL format”. Regardless, you should be exchanging objects with your database rather than mere strings. Look at the getObject and setObject methods in JDBC when using a driver compliant with JDBC 4.2 or later.

Database

For SQL access to a database column of SQL-standard type DATE, use JDBC 4.2 or later to pass the LocalDate object directly. Do not use mere strings to exchange date-time values.

myPreparedStatement.setObject( … , ld ) ;

Retrieval.

LocalDate ld = myResultSet.getDate( … , LocalDate.class ) ;

Table of date-time types in Java (both modern and legacy) and in standard SQL.


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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
3
int dayOfYear = 112;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);
SimpleDateFormat sdf = new SimpleDateFormat("YYYY/MM/dd");
System.out.println("Day of year " + dayOfYear + " = " + sdf.format(calendar.getTime()));

hope above snippets helps you

ganesh r
  • 51
  • 5
  • Thanks for the answer,i'll try this and i'll let you know if it helps! – Asjon Nov 04 '15 at 10:25
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 01 '18 at 00:20