Saturday, 20 August 2022

Java: Convert java.util.Date to calendar

Using Calendar#setTime method, we can set the calendar time to given date.

 

Find the below working application.


 

DateToCalendar.java

package com.sample.app;

import java.util.Calendar;
import java.util.Date;

public class DateToCalendar {
    
    public static void main(String[] args) {
        Date date = new Date();
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        
        System.out.println("date in millis : " + date.getTime());
        System.out.println("calendar in millis : " + calendar.getTimeInMillis());
    }

}

 

Output

date in millis : 1661061743001
calendar in millis : 1661061743001

 

  

Previous                                                 Next                                                 Home

No comments:

Post a Comment