Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Date based on day count in Java

Simple Question, but Google surprisingly had little on this. I have the number of days from Jan 1st of the year. How can I convert that to a date in Java?

like image 204
J86 Avatar asked Dec 11 '22 15:12

J86


1 Answers

You can simply use SimpleDateFormat to convert String to Date. The pattern D can be used to represent the day number of year.

Provided that you've an

int numberOfDays = 42; // Arbitrary number.

then this one counts the number of days since 1 Jan 1970 (the Epoch)

Date date = new SimpleDateFormat("D").parse(String.valueOf(numberOfDays));

alternatively, this one counts the number of days since 1 Jan of current year.

Date date = new SimpleDateFormat("D yyyy").parse(numberOfDays + " " + Calendar.getInstance().get(Calendar.YEAR));
like image 117
BalusC Avatar answered Dec 14 '22 04:12

BalusC