December 29 2016

Java calendar day of year

Java calendar day of year

Set the Calendar object to the date for which you need the calendar day of year, Calendar .get(Calendar.DAY_OF_YEAR) will give you day number within the current year.

Example

package com.answersz;

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

public class JavaCalendarDayOfYear {

 public static void main(String[] args) throws ParseException {

 // Java calendar day of year

 String date = "29-12-2016";

 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
 Date dt = sdf.parse(date);

 Calendar cal = Calendar.getInstance();
 cal.setTime(dt);

 int DAY_OF_YEAR = cal.get(Calendar.DAY_OF_YEAR);

 System.out.println("Date : " + dt);
 System.out.println("Day of Year : " + DAY_OF_YEAR);

 }

}

Output


Date : Thu Dec 29 00:00:00 IST 2016
Day of Year : 364


Copyright © 2014. All rights reserved.

Posted December 29, 2016 by answersz in category Core Java

Leave a Comment

Your email address will not be published. Required fields are marked *

*