Showing posts with label calendar. Show all posts
Showing posts with label calendar. Show all posts

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

Java: Get time in milliseconds from Date, Calendar and ZonedDateTime

Get time in milliseconds from Date

Date#getTime() method return the time in milliseconds.

Date date = new Date();
long timeMillis1 = date.getTime();

 

Get time in milliseconds from Calendar

Calendar#getTimeInMillis() method return the time in milliseconds.
Calendar calendar = Calendar.getInstance();
long timeMillis2 = calendar.getTimeInMillis();

Get time in milliseconds from ZonedDateTime

ZonedDateTime zonedDateTime = ZonedDateTime.now();
Instant instant = zonedDateTime.toInstant();
long timeMillis3 = instant.toEpochMilli();

Find the below working application.

 


TimeInMilliseconds.java

package com.sample.app;

import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;

public class TimeInMilliseconds {

    public static void main(String[] args) {
        Date date = new Date();
        long timeMillis1 = date.getTime();

        Calendar calendar = Calendar.getInstance();
        long timeMillis2 = calendar.getTimeInMillis();

        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        Instant instant = zonedDateTime.toInstant();
        long timeMillis3 = instant.toEpochMilli();

        System.out.println("timeMillis1 : " + timeMillis1);
        System.out.println("timeMillis2 : " + timeMillis2);
        System.out.println("timeMillis3 : " + timeMillis3);
    }

}

Output

timeMillis1 : 1661059945266
timeMillis2 : 1661059945284
timeMillis3 : 1661059945323





 

 

 

 

 

Previous                                                 Next                                                 Home

Sunday, 10 July 2022

Print all the current week dates in Java

Using ‘java.util.Calendar’ class, we can get all the current week dates in Java.

 


CurrentWeekDates.java

package com.sample.app;

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

public class CurrentWeekDates {

	private static void printAllCurrentWeekDates() {
		Calendar calendar = Calendar.getInstance();

		for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
			calendar.set(Calendar.DAY_OF_WEEK, i);
			System.out.println("Date " + calendar.getTime());
		}

	}

	public static void main(String[] args) {
		System.out.println("today : " + new Date() +"\n\n");
		printAllCurrentWeekDates();
	}

}

Sample Output

today : Mon Jul 11 09:56:33 IST 2022


Date Sun Jul 10 09:56:33 IST 2022
Date Mon Jul 11 09:56:33 IST 2022
Date Tue Jul 12 09:56:33 IST 2022
Date Wed Jul 13 09:56:33 IST 2022
Date Thu Jul 14 09:56:33 IST 2022
Date Fri Jul 15 09:56:33 IST 2022
Date Sat Jul 16 09:56:33 IST 2022


 

Previous                                                 Next                                                 Home

How to get current weeks Tuesday date

Following program return the current weeks Tuesday date.

 


CurrentWeekTuesdayDate.java

package com.sample.app;

import java.util.Calendar;

public class CurrentWeekTuesdayDate {

	public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
		
		System.out.println("Date " + calendar.getTime());

	}

}

Output

Date Sat Jul 09 21:11:19 IST 2022


 

Previous                                                 Next                                                 Home

Friday, 22 October 2021

Java: date and time programs

 

      Convert time stamp to Date Format
      Convert yyyy-MM-dd HH:mm:ss.SSS format to date object
      Convert yyyy-MM-dd HH:mm:ss.SSS format date to long value
      Convert long value to yyyy-MM-dd HH:mm:ss.SSS format
      Get year, date, day, time from Calendar
      Java: Get number of weeks between two dates
      Java: Get number of months between two dates
      Java: Get number of days between two dates
      Java: Get number of years between two dates
      Java: Number of hours between two dates
      Java: Get number of minutes between two dates
      Java: get number of seconds between two dates
      How to add days to a date in Java?
      Java: Check whether given date is valid or not
      Java: Increment the date by one day
      Java: Get date and time for given time zone
      Java: Get all supported time zones
      Get milliseconds from LocalDateTime
      Get Date from Unix timestamp
      Get Instant from Unix time stamp
      Get Calendar from Unix time stamp
      Set Default time zone to the application
      How to set time zone for java.util.Date
      Format Instant to String
      Get LocalDate from long epoch time
      Get LocalDateTime from long epoch time
      Compare two dates without time portion
      Java Convert LocalDate to Date
      Java program to display date and time
      Java: Convert the current time to HOURS:MINUTES:SECONDS:MILLISECONDS format
      How to get java.sql.TimeStamp in Java?
      How to set JVM timezone?
      Java: Is LocalDate has time information?
      Java: convert milliseconds to Date object
      Java: Get start time of next month date
      java: Get start time of next week date
      java: Get start time of next day
      Java: Get the start time of next hour
      Java: Get start time of next minute
      Java: Get start time of next second
      Get Date object from year, month and day
      How to get current weeks Tuesday date?
      Print all the current week dates in Java
      Convert the date string yyyy-MM-dd to dd-MM-yyyy in Java?
      Convert date or time from one time zone to other
      Java: Get time in milliseconds from Date, Calendar and ZonedDateTime
      Java: Get milliseconds from given date and time
      Java: Convert java.util.Date to calendar
      Java: Get current date and time using LocalDate, LocalTime, LocalDateTime
      Java: Convert time from 12 hour format to 24 hour format
      Java: Convert time from 24 hour format to 12 hour format
      Convert java.util.Date to java.sql.Date in Java
      Convert java.sql.Date to java.util.Date in Java
      Get the current date, with the milliseconds removed
      Mastering Date-Time Formatting in Java with DateTimeFormatterBuilder
      Utility class to convert the time in double to different time formats like days, hours, minutes, seconds etc.,

Previous                                                    Next                                                    Home

Sunday, 12 April 2020

Convert Calendar to yyyy-MM-dd format

Using 'SimpleDateFormat', you can convert Calendar instance to 'yyyy-MM-dd' format.

Example
Calendar cal = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String date = simpleDateFormat.format(cal.getTime());

App.java
package com.sample.app;

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

public class App {

 public static void main(String args[]) {
  Calendar cal = Calendar.getInstance();

  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

  String date = simpleDateFormat.format(cal.getTime());

  System.out.println("date : " + date);
  
 }
}

Output
date : 2020-01-25

You may like

Monday, 9 March 2020

Get Calendar from Unix time stamp

Unix time is the number of seconds that have elapsed since 00:00:00, 1 January 1970, UTC, without counting the leap seconds.

You can get Unix time stamp by executing the command date '+%s' on terminal.

$date '+%s'
1577603360

Below snippet convers unix time stamp to Calendar.

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(unixTimestamp * 1000);

App.java
package com.sample.app;

import java.util.Calendar;

public class App {

 public static void main(String[] args) {
  long unixTimestamp = 1577603360;

  Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(unixTimestamp * 1000);

  System.out.println(calendar.getTime());

 }

}

Output
Sun Dec 29 12:39:20 IST 2019



You may like
Previous                                                    Next                                                    Home