Sunday, 6 March 2022

Java: Get start time of next second

Below snippet return the start time of next second.

public static long getStartOfNextSecond(long now) {
	Calendar calendar = Calendar.getInstance();

	calendar.setTime(new Date(now));
	calendar.set(Calendar.MILLISECOND, 0);
	calendar.add(Calendar.SECOND, 1);

	return calendar.getTime().getTime();
}

 

Find the below working application.

 

GetStartOfNextSecond.java

package com.sample.app.time;

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

public class GetStartOfNextSecond {
	public static long getStartOfNextSecond(long now) {
		Calendar calendar = Calendar.getInstance();

		calendar.setTime(new Date(now));
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.add(Calendar.SECOND, 1);

		return calendar.getTime().getTime();
	}

	public static void main(String args[]) {
		Date today = new Date();

		long startOfNextSecondInMillis = getStartOfNextSecond(today.getTime());

		Date startOfNextSecond = new Date(startOfNextSecondInMillis);

		System.out.println("today : " + today);
		System.out.println("startOfNextSecond : " + startOfNextSecond);

	}
}

 

Output

today : Sat Feb 26 12:19:16 IST 2022
startOfNextSecond : Sat Feb 26 12:19:17 IST 2022

 

 

 

Previous                                                    Next                                                    Home

Java: Get start time of next minute

Below snippet return the start time of next minute.

public static long getStartOfNextMinute(long now) {
	Calendar calendar = Calendar.getInstance();
	
	calendar.setTime(new Date(now));
	calendar.set(Calendar.MILLISECOND, 0);
	calendar.set(Calendar.SECOND, 0);
	calendar.add(Calendar.MINUTE, 1);
	
	return calendar.getTime().getTime();
}

 

Find the below working application.

 

GetStartOfNextMinute.java

 

package com.sample.app.time;

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

public class GetStartOfNextMinute {
	public static long getStartOfNextMinute(long now) {
		Calendar calendar = Calendar.getInstance();
		
		calendar.setTime(new Date(now));
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.add(Calendar.MINUTE, 1);
		
		return calendar.getTime().getTime();
	}

	public static void main(String args[]) {
		Date today = new Date();

		long startOfNextMinuteInMillis = getStartOfNextMinute(today.getTime());

		Date startOfNextMinute = new Date(startOfNextMinuteInMillis);

		System.out.println("today : " + today);
		System.out.println("startOfNextMinute : " + startOfNextMinute);

	}
}

Output

today : Sat Feb 26 12:12:12 IST 2022
startOfNextMinute : Sat Feb 26 12:13:00 IST 2022

 

 

 



Previous                                                    Next                                                    Home

Java: Get the start time of next hour

Below snippet return the start time of next hour.

public static long getStartOfNextHour(long now) {
	Calendar calendar = Calendar.getInstance();

	calendar.setTime(new Date(now));
	calendar.set(Calendar.MILLISECOND, 0);
	calendar.set(Calendar.SECOND, 0);
	calendar.set(Calendar.MINUTE, 0);
	calendar.add(Calendar.HOUR, 1);

	return calendar.getTime().getTime();
}

Find the below working application.

 

GetStartOfNextHour.java

package com.sample.app.time;

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

public class GetStartOfNextHour {
	public static long getStartOfNextHour(long now) {
		Calendar calendar = Calendar.getInstance();

		calendar.setTime(new Date(now));
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.add(Calendar.HOUR, 1);

		return calendar.getTime().getTime();
	}

	public static void main(String args[]) {
		Date today = new Date();

		long startOfNextHourInMillis = getStartOfNextHour(today.getTime());

		Date startOfNextHour = new Date(startOfNextHourInMillis);

		System.out.println("today : " + today);
		System.out.println("startOfNextHour : " + startOfNextHour);

	}
}

Output

today : Sat Feb 26 10:21:25 IST 2022
startOfNextHour : Sat Feb 26 11:00:00 IST 2022






 

 

Previous                                                    Next                                                    Home

java: Get start time of next day

Below snippet return the start of next day.

public static long startOfNextDay(long now) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(new Date(now));

	calendar.add(Calendar.DAY_OF_MONTH, 1);
	calendar.set(Calendar.MILLISECOND, 0);
	calendar.set(Calendar.SECOND, 0);
	calendar.set(Calendar.MINUTE, 0);
	calendar.set(Calendar.HOUR_OF_DAY, 0);

	return calendar.getTime().getTime();
}

 

Find the below working application.

 

GetStartOfNextDay.java

 

package com.sample.app.time;

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

public class GetStartOfNextDay {
	public static long startOfNextDay(long now) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(now));

		calendar.add(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.HOUR_OF_DAY, 0);

		return calendar.getTime().getTime();
	}

	public static void main(String args[]) {
		Date today = new Date();

		long startOfNextDayInMillis = startOfNextDay(today.getTime());

		Date startOfNextDay = new Date(startOfNextDayInMillis);

		System.out.println("today : " + today);
		System.out.println("startOfNextDay : " + startOfNextDay);

	}

}

 

Output

today : Sat Feb 26 10:07:23 IST 2022
startOfNextDay : Sun Feb 27 00:00:00 IST 2022

 

 

 

 

 

 

Previous                                                    Next                                                    Home

java: Get start time of next week date

Below snippet return the start of next week date.

public static long startStartOfNextWeek(long now) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(new Date(now));

	calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
	calendar.set(Calendar.HOUR_OF_DAY, 0);
	calendar.set(Calendar.MINUTE, 0);
	calendar.set(Calendar.SECOND, 0);
	calendar.set(Calendar.MILLISECOND, 0);
	calendar.add(Calendar.WEEK_OF_YEAR, 1);

	return calendar.getTime().getTime();
}

 

Find the below working application.

 

GetStartOfNextWeek.java

package com.sample.app.time;

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

public class GetStartOfNextWeek {

	public static long startStartOfNextWeek(long now) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(now));

		calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.add(Calendar.WEEK_OF_YEAR, 1);

		return calendar.getTime().getTime();
	}

	public static void main(String args[]) {
		Date today = new Date();

		long startOfNextWeek = startStartOfNextWeek(today.getTime());

		Date startOfNextWeekDate = new Date(startOfNextWeek);

		System.out.println("today : " + today);
		System.out.println("startOfNextWeekDate : " + startOfNextWeekDate);

	}
}

 

Output

today : Sat Feb 26 09:57:07 IST 2022
startOfNextWeekDate : Sun Feb 27 00:00:00 IST 2022

 

 

 

 

 

Previous                                                    Next                                                    Home

Java: Get start time of next month date

Below snippet return the start of next month date.

public static long startOfNextMonth(long now) {
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(new Date(now));

	calendar.set(Calendar.DATE, 1);
	calendar.set(Calendar.HOUR_OF_DAY, 0);
	calendar.set(Calendar.MINUTE, 0);
	calendar.set(Calendar.SECOND, 0);
	calendar.set(Calendar.MILLISECOND, 0);
	calendar.add(Calendar.MONTH, 1);
	
	return calendar.getTime().getTime();
}

Find the below working application.

 

GetStartOfNextMonth.java

package com.sample.app.time;

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

public class GetStartOfNextMonth {

	public static long startOfNextMonth(long now) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(now));

		calendar.set(Calendar.DATE, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.add(Calendar.MONTH, 1);
		
		return calendar.getTime().getTime();
	}

	public static void main(String args[]) {
		Date today = new Date();

		long startOfNextMonth = startOfNextMonth(today.getTime());

		Date startOfNextMonthDate = new Date(startOfNextMonth);

		System.out.println("today : " + today);
		System.out.println("startOfNextMonthDate : " + startOfNextMonthDate);

	}

}

Output

today : Sat Feb 26 09:38:25 IST 2022
startOfNextMonthDate : Tue Mar 01 00:00:00 IST 2022



    


 

 

Previous                                                    Next                                                    Home

Java: convert milliseconds to Date object

Date class provides a constructor 'Date(long time)', that takes time in milliseconds and return Date object from it.

 

Signature

public Date(long time)

Find the below working application.

 

MilliSecondsToDate.java

package com.sample.app.time;

import java.util.Date;

public class MilliSecondsToDate {

	public static void main(String[] args) {
		Date today = new Date();

		long timeAfterOneHour = today.getTime() + 60 * 60 * 1000;

		Date timeAfterAnHour = new Date(timeAfterOneHour);

		System.out.println("today : " + today);
		System.out.println("timeAfterAnHour : " + timeAfterAnHour);

	}

}

Output

today : Sat Feb 26 09:47:30 IST 2022
timeAfterAnHour : Sat Feb 26 10:47:30 IST 2022




 

  

Previous                                                    Next                                                    Home