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);
Related
I have a date say for e.g. current date which is 19/04/2013
And I have number of months given say for e.g. 10
I want to find out the date falling before 10 months from 19/04/2013.
How to achieve it in Java?
For example, if I want to find out date before a week, I can achieve as below:
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -7);
Date newDate = calendar.getTime();
But how to find the same for months ?
Well, you could do something similar to your example:
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.MONTH, -10);
Date newDate = calendar.getTime();
It'll set newDate to a date 10 months before myDate.
Using jodatime this is so much easier and verbose:
LocalDate date = LocalDate.parse("19/04/2013", DateTimeFormat.forPattern("dd/MM/yyyy"));
LocalDate result = date.minus(Months.TEN);
System.out.println(result.toString("dd/MM/yyyy"));
I want to convert the calendar object between two time zones in java.I shall the pass the first calendar object and want the output to be the modified calendar object with the different timezone.
Can someone provide me a way on how to do it ?
This is what i have done ...
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
GregorianCalendar pst = new GregorianCalendar();
GregorianCalendar est = new GregorianCalendar();
pst.setTime(maintWindow);
int year = pst.get(Calendar.YEAR);
int month = pst.get(Calendar.MONTH);
int day = pst.get(Calendar.DAY_OF_MONTH);
format.setTimeZone(timeZone);
pst.set(year, month, day, hour, min);
Date date = pst.getTime();
logger.info(date);
logger.info(format.format(date));
logger.info(pst.getTime());
est.setTimeInMillis(date.getTime());
logger.info(est.getTime());
You can use Calendar.setTimeZone.
For example if you have a Calendar reference cal initialized with any time zone, a call to the method like this
cal.setTimeZone(TimeZone.getTimeZone("GMT"))
modifies the time zone of cal to GMT.
I'm facing some problems in comparing the current date and the date which is retrieved from Database.I just retrieved date from DataBase and Stored in a Date variable like this
String due_date_task = cursor.getString(cursor.getColumnIndex(dueDateOfTask));
SimpleDateFormat currentFormater = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = currentFormater.parse(due_date_task);
Now,what i want is should check whether date which is retrieved from DataBase is Equivalent to CurrentDate or not.
Calendar currentDate = Calendar.getInstance();
Date date2 = currentDate.getTime();
if(date1.equals(date2))
System.out.println("Today Task");
i just want to check like this.Thanks in advance
For exact match including milliseconds, use getTime:
if(date.getTime() == date1.getTime()){
//do something
}
You can use this function:
private boolean compareDates(Calendar objCal1, Calendar objCal2) {
return ((objCal1.get(Calendar.YEAR) == objCal2.get(Calendar.YEAR))
&& (objCal1.get(Calendar.DAY_OF_YEAR) == objCal2.get(Calendar.DAY_OF_YEAR)));
}
creating the calendar objects:
Calendar objCal1 = new GregorianCalendar().setTime(date);
Calendar objCal2 = new GregorianCalendar().setTime(date1);
Try this way to get the current date,
Calendar calCurr = Calendar.getInstance();
Log.i("Time in mili of Current - Normal", ""+calCurr.getTimeInMillis()); // see what it gives? dont know why?
Date date = new Date();
calCurr.set(date.getYear()+1900, date.getMonth()+1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());// so added one month to it
Log.i("Time in mili of Current - after update", ""+calCurr.getTimeInMillis()); // now get correct
now create Calendar object for database value,
String due_date_task = cursor.getString(cursor.getColumnIndex(dueDateOfTask));
SimpleDateFormat currentFormater = new SimpleDateFormat("dd/MM/yyyy");
Date date = currentFormater.parse(due_date_task);
Calendar start = Calendar.getInstance();
start.set(date.getYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
and now Compare both the Calendar objects
if(calCurr.equals(start))
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());