Set Calendar Date to Current Date - java

I have variable with value of timeInMills which is past 3 days ago, I wanted to reset the date of it to current date but the time should be still.
Calendar calNow = Calendar.getInstance();
Calendar calSets = (Calendar)calNow.clone();
calSets.setTimeInMillis(TIME_IN_MILL); //set datetime from timeInMillis
//Reset the date to current Date.
How to do that?

Like this, get the properties you want, before you change the instance:
Calendar calNow = Calendar.getInstance();
Calendar calSets = (Calendar)calNow.clone();
int hours = calNow.get(Calendar.HOUR_OF_DAY)
int minutes = calNow.get(Calendar.MINUTE)
calSets.setTimeInMillis(TIME_IN_MILL); //set datetime from timeInMillis
//Reset the date to current Date.
calSets.set(Calendar.SECOND, 0);
calSets.set(Calendar.MILLISECOND, 0);
calSets.set(Calendar.HOUR_OF_DAY, hours);
calSets.set(Calendar.MINUTE, minutes);

You can reset a Calendar by calling setTimeInMillis(System.currentTimeMillis()):
TimeZone.setDefault(TimeZone.getTimeZone("UTC")); // Just for testing
final long TIME_IN_MILL = 1563204600000L; // 2019-07-15 15:30 UTC
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(TIME_IN_MILL);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
cal.setTimeInMillis(System.currentTimeMillis()); // Reset
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(cal.getTime()));
The code prints 2019-07-18 15:30:00.000, which is todays date with the time of day from the TIME_IN_MILL value.
If you don't want to rely on System.currentTimeMillis(), just get the value from the Calendar object, first thing:
Calendar cal = Calendar.getInstance();
long now = cal.getTimeInMillis();
cal.setTimeInMillis(TIME_IN_MILL);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
cal.setTimeInMillis(now);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

Related

how to get the elapsed time from the beginning of a month to now in android programmatically

I want to to get the elapsed time from the beginning of a month to now in android programmatically.
preferably using Calendar.getInstance().
For example today is 12/10/2018. so the duration in millisecs will be 12/01/2018 to 12/10/2018
To retrieve the beginning of the month:
val cal = Calendar.getInstance()
cal.set(Calendar.HOUR_OF_DAY, 0)
cal.clear(Calendar.MINUTE)
cal.clear(Calendar.SECOND)
cal.clear(Calendar.MILLISECOND)
cal.set(Calendar.DAY_OF_MONTH, 1)
Then to calculate elapsed in milliseconds:
val current = Calendar.getInstance()
val timePassedMilliseconds=current.timeInMillis-cal.timeInMillis
is your problem creating a new Calendar and populating it? you can create an empty one, and just populate with the fields that you are interested in.
Calendar now = Calendar.getInstance();
Calendar startOfMonth = new GregorianCalendar();
calendar.set(Calendar.DAY_OF_MONTH, 1); //first day of month
calendar.set(Calendar.MONTH, now.get(Calendar.MONTH));
calendar.set(Calendar.YEAR, now.get(Calendar.YEAR);
timeElapsed = now.getTimeInMillis() - startOfMonth.getTimeInMillis() ;
You can use calendar.set(year,month,1,0,0,0); to get the timestamp of the first day of the month.
Calendar calendar = Calendar.getInstance();
Date d = new Date(1544371200000L); //12/10/2018
calendar.setTime(d);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
calendar.set(year,month,1,0,0,0);
Date firstDayOfMonth = calendar.getTime();
long duration = d.getTime() - firstDayOfMonth.getTime();

Get time as year/month/day/hour/minutes and convert to millisecond like getTime() in java or android

I want to get time from user(with editbox like year/month/day/hour/minutes).
How can i convert it to millisecond like getTime function or convert to minutes like getTime()/60000?
You can use a DatePicker and then use the getters from that to either make a Calendar object or to get the time in millis.
I use this function in my projects to get the Calendar from the DatePicker, you may want to extend it to set the hours/ minutes as well:
public static Calendar getCalenderFromDatePicker(DatePicker dp) {
int day = dp.getDayOfMonth();
int month = dp.getMonth();
int year = dp.getYear();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
return calendar;
}
Once you have the Calendar you can use myCalendar.getTimeInMillis() which returns a long of the time in millis since the epoch.
Consider java.util.Calendar class and the following code:
Calendar calendar = Calendar.getInstance();
int year = 2015;
int month = 4; // "May", months are counting from 0 to 11
int day = 14;
int hour = 14;
int minutes = 15;
int seconds = 16;
// for more clarity I set year/day/month/hours/minutes/seconds separately
// you can also set them at once via calendar.set(year, month, day);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, seconds);
long composedDateInMilliseconds = calendar.getTimeInMillis();

How to get the today's date / time using the TimePicker?

I've a TimePicker to get time. And I've following Java code for an Android Application :
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Log.e("Alarm", "" + calendar.getTime());
calendar.set(Calendar.HOUR, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
Log.e("Alarm", "" + calendar.getTime());
The second process gives a day ahead than first process.
I've tried following code and subtracted the one day.
calendar.add(Calendar.DATE, -1);
Is this the only way or best way? How to get the today's date / time using the TimePicker?
Check the following code.....may be you get help from this
public void setCurrentTimeOnView() {
tvDisplayTime = (TextView) findViewById(R.id.tvTime);
timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
// set current time into textview
tvDisplayTime.setText(
new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));
// set current time into timepicker
timePicker1.setCurrentHour(hour);
timePicker1.setCurrentMinute(minute);
}
OK, I got the solution. Thank you everyone! :)
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
Log.e("Alarm", "" + calendar.getTime());
You should add: calendar.getInstance(); instead calendar.add(Calendar.DATE, -1);
My code:
Date today = new Date();
final Calendar c = Calendar.getInstance();
preventa.setText(VarGlobales.formatofecha(today));
Time Picker gives the GMT time, you should also take TimeZone offset into account.

Calculate time difference between current and future time

I want to calculate time difference in milliseconds from current time of a day(11 am , 1 october,2012) and time at midnight for the same day (11 pm 59 m 59s , 1 october , 2012.
I have tried this
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 59);
cal.add(Calendar.HOUR, 23);
cal.add(Calendar.MINUTE, 59);
cal.getTime().getTime() - today.getTime();
here today is the current date.
But when i print long values of cal and today , the time difference if of 86400 approx one day.
Use cal.set() instead of cal.add()
Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.HOUR, 23);
cal.set(Calendar.MINUTE, 59);
long diff = cal.getTime().getTime() - today.getTime();
You can set your date to newly created Calendar instance..
And then compare it with current instance using getTimeInMillis()
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.DATE, 1);
cal.set(Calendar.MONTH, 9);
cal.set(Calendar.YEAR, 2012);
long difference = cal.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();

How in Java find dates for previous 2 mondays?

May be like this:
for(int i=0;i<15;i++){
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_MONTH, -1);
if (cal.Calendar.DAY_OF_WEEK==1){
System.out.println(cal.cal.getTime())
But may be exists more simple way?
Thanks.
You are on the right track.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, -7); // First week before
cal.add(Calendar.DAY_OF_YEAR, -7); // Second week before
Let me make this work for just Mondays.
Calendar cal = Calendar.getInstance();
int weekday = cal.get(Calendar.DAY_OF_WEEK);
int days = (Calendar.SATURDAY - weekday + 2) % 7;
cal.add(Calendar.DAY_OF_YEAR, days);
cal.add(Calendar.DAY_OF_MONTH, -7);
cal.add(Calendar.DAY_OF_MONTH, -7);
Even simpler would be to set the weekday directly:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.add(Calendar.DATE, -7);
System.out.println(cal.getTime());
Please keep in mind, that this does not effect the time. If you want 00:00, you need to set the appropriate values:
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
java.time
Use a TemporalAdjuster.
LocalDate today = LocalDate.now();
LocalDate previousOrSameMonday = today.with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) );
And subtract a week to get a second one.
LocalDate secondMondayBefore = previousOrSameMonday.minusWeeks( 1 );

Categories