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?
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With