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 );
Related
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);
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();
I need to get the dates of last two Fridays using today's date.This is the code I'm currently using
public class GetDate {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dt = new Date();
Calendar c = Calendar.getInstance();
c.setTime(dt);
System.out.println("today : " + sdf.format(dt));
while (c.get(Calendar.DAY_OF_WEEK) != 6) {
c.add(Calendar.DATE, -1);
}
Date lastFri=c.getTime();
System.out.println("last fri : "+sdf.format(lastFri));
c.add(Calendar.DATE, -7);
Date prevFri = c.getTime();
System.out.println("previous friday : "+sdf.format(prevFri));
}
}
Is there any way to optimize this code??
With Java 8, and if you don't have to use the Calendar api, you can use a TemporalAdjuster:
LocalDate today = LocalDate.now();
LocalDate prevFriday = today.with(previous(FRIDAY));
LocalDate prevPrevFriday = prevFriday.with(previous(FRIDAY));
note: requires the following static import
import static java.time.temporal.TemporalAdjusters.previous;
import static java.time.DayOfWeek.FRIDAY;
You could get the last Friday this way :
Calendar cal = Calendar.getInstance();
cal.add(Calendar.WEEK_OF_YEAR, -1);
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
This gets the last Friday by subtracting a week and setting the day of week to Friday.
Try something like:
cal.add(Calendar.WEEK_OF_YEAR, -1);
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
Date lastFriday = cal.getTime();
cal.add(Calendar.WEEK_OF_YEAR, -1);
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
Date lastToLastFriday = cal.getTime();
This line should help:
calendar.add(Calendar.DATE, Calendar.FRIDAY - 7 - c.get(Calendar.DAY_OF_WEEK));
So this code is solving your problem:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = GregorianCalendar.getInstance();
c.add(Calendar.DATE, Calendar.FRIDAY - 7 - c.get(Calendar.DAY_OF_WEEK));
System.out.println(sdf.formate(c.getTime()));
c.add(Calendar.DATE, Calendar.FRIDAY - 14 - c.get(Calendar.DAY_OF_WEEK));
System.out.println(sdf.formate(c.getTime()));
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();
I used this to create a date and put it into a database:
String currentDateTimeString = DateFormat.getDateInstance().format(new Date());
I want to build a date with a similar format to the one above and compare it with currentDateTimeString.
I have 3 integers. How do I do that (int year, int month, int day)?
UPDATE:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_MONTH, day);
Date result = cal.getTime();
String currentDateTimeString2 = DateFormat.getDateInstance().format(result);
I do here something wrong..both arent equal:
currentDateTimeString2==currentDateTimeString //false
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, date);
cal.set(Calendar.MONTH, month-1);//it starts from 0
cal.set(Calendar.YEAR, year);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date result = cal.getTime();
I do here something wrong..both arent equal: currentDateTimeString2==currentDateTimeString //false
String are object, it can't be compared with == use equals() method
Dates are represented in milliseconds. new Date returns the current date, and by date it means year, month, day, hour, minute, second, millisecond.
Comparison can be made via Date.isAfter.
Don't use Strings to compare Dates. Don't use == to compare Objects.