I'm designing a program where in my recylerview i wanted to display a list of items whose date is 4 days less or more than current date.
This will work for month:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);
This will work for -4 day:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -4);
Related
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 date based on the following condition,
From the current date, If the user selects only date(1-31), must subtract it from the current date and get the new date.
If the user selects weeks(1-53) then we have to remove the selected no'of weeks from the current dates no'of weeks and get a new date.
If the user selects only months(1-12) then we have to delete the selected number of months from the current date months and get a new date.
Sample code for date checking..
Calendar today=Calendar.getInstance();
int month=today.MONTH;
int year=today.YEAR;
today.clear();
today.set(year, month,dateOfMonth);
date=today.getTime();
Any Logic is highly appreciated.
Thanks in Advance.
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(currentDate);
if (inDays) {
cal.add(Calendar.DATE, -n);
} else if (inWeeks) {
cal.add(Calendar.WEEK_OF_YEAR, -n);
} else if (inMonths) {
cal.add(Calendar.MONTH, -n);
}
You can achieve all of this using :
java.util.Calendar.add(int, int)
For instance:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -10); // substracts 10 days from 'now'
Use JodaTime.
Add days to a date:
http://joda-time.sourceforge.net/apidocs/org/joda/time/Days.html#plus(int)
I have some date and I want to get last x day before this date so this is my code:
Calendar today = Calendar.getInstance();
today.add(Calendar.DATE, -x);
date = new Date(today.getTimeInMillis()))
this code works only if some day is actual day. How I can change it. Is there some method to get calendar from date ?
Use the setTime method to set the date of your calendar :
Calendar aDay = Calendar.getInstance();
aDay.setTime(aDate);
I am having issues with the calculation of when the next Last Day of the Month is for a notification which is scheduled to be sent.
Here is my code:
RecurrenceFrequency recurrenceFrequency = notification.getRecurrenceFrequency();
Calendar nextNotifTime = Calendar.getInstance();
This is the line causing issues I believe:
nextNotifTime.add(recurrenceFrequency.getRecurrencePeriod(),
recurrenceFrequency.getRecurrenceOffset());
How can I use the Calendar to properly set the last day of the next month for the notification?
Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);
This returns actual maximum for current month. For example it is February of leap year now, so it returns 29 as int.
java.time.temporal.TemporalAdjusters.lastDayOfMonth()
Using the java.time library built into Java 8, you can use the TemporalAdjuster interface. We find an implementation ready for use in the TemporalAdjusters utility class: lastDayOfMonth.
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
LocalDate now = LocalDate.now(); //2015-11-23
LocalDate lastDay = now.with(TemporalAdjusters.lastDayOfMonth()); //2015-11-30
If you need to add time information, you may use any available LocalDate to LocalDateTime conversion like
lastDay.atStartOfDay(); //2015-11-30T00:00
And to get last day as Date object:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
Date lastDayOfMonth = cal.getTime();
You can set the calendar to the first of next month and then subtract a day.
Calendar nextNotifTime = Calendar.getInstance();
nextNotifTime.add(Calendar.MONTH, 1);
nextNotifTime.set(Calendar.DATE, 1);
nextNotifTime.add(Calendar.DATE, -1);
After running this code nextNotifTime will be set to the last day of the current month. Keep in mind if today is the last day of the month the net effect of this code is that the Calendar object remains unchanged.
Following will always give proper results:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, ANY_MONTH);
cal.set(Calendar.YEAR, ANY_YEAR);
cal.set(Calendar.DAY_OF_MONTH, 1);// This is necessary to get proper results
cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
cal.getTime();
You can also use YearMonth.
Like:
YearMonth.of(2019,7).atEndOfMonth()
YearMonth.of(2019,7).atDay(1)
See
https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html#atEndOfMonth--
Using the latest java.time library here is the best solution:
LocalDate date = LocalDate.now();
LocalDate endOfMonth = date.with(TemporalAdjusters.lastDayOfMonth());
Alternatively, you can do:
LocalDate endOfMonth = date.withDayOfMonth(date.lengthOfMonth());
Look at the getActualMaximum(int field) method of the Calendar object.
If you set your Calendar object to be in the month for which you are seeking the last date, then getActualMaximum(Calendar.DAY_OF_MONTH) will give you the last day.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = sdf.parse("11/02/2016");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println("First Day Of Month : " + calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
System.out.println("Last Day of Month : " + calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Kotlin date extension implementation using java.util.Calendar
fun Date.toEndOfMonth(): Date {
return Calendar.getInstance().apply {
time = this#toEndOfMonth
}.toEndOfMonth().time
}
fun Calendar.toEndOfMonth(): Calendar {
set(Calendar.DAY_OF_MONTH, getActualMaximum(Calendar.DAY_OF_MONTH))
return this
}
You can call toEndOfMonth function on each Date object like Date().toEndOfMonth()
I want a javascript or java program should always give date 1st of current month.
Is there any tech?
You can use Calendar for Java
Date date = new Date(System.currentTimeMillis());
cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
Now you do what every you want to do with this Calendar object like to get the Day of the Week (Sat, Sun, .... )
int weekday = cal.get(Calendar.DAY_OF_WEEK);
And for JavaScript you can use:
var theFirst = new Date();
theFirst.setDate(1);
setDate sets the day of the month for the Date object (from 1 to 31). Then you can do whatever you want with theFirst, like get the day of the week.
Calendar ans = Calendar.getInstance();
ans.set(ans.get(Calendar.YEAR),
ans.get(Calendar.MONTH),
1,
0,
0,
0
);
System.out.println(ans.getTime());