Date toDate = new Date(114, 5, 30);
Calendar calendar = Calendar.getInstance();
Calendar calendarTo = Calendar.getInstance();
calendar.setTime(toDate);
calendarTo.setTime(toDate);
calendarTo.add(Calendar.DATE, 1);
this is how I initialize calendars and I am trying to put NEXT day in calendarTo
but when I getting calendar.Date it is equal to calendarTo.DATE and is equal to 5.. why?
And how I could finally increment this DATE value?
What you are getting is the default value of DATE in Calendar class. Which is 5
public final static int DATE = 5;
But when I print the dates from your code, looks like it is fine.
Date toDate = new Date(114, 5, 30);
Calendar calendar = Calendar.getInstance();
Calendar calendarTo = Calendar.getInstance();
calendar.setTime(toDate);
calendarTo.setTime(toDate);
calendarTo.add(Calendar.DATE, 1);
System.out.println(toDate);//Mon Jun 30 00:00:00 IST 2014
System.out.println(calendarTo.getTime());//Tue Jul 01 00:00:00 IST 2014
Related
I have to generate days between two two dates. for ex:
1420090495000 -> Jan 1, 2015
1430458495000 -> May 1, 2015
I have to generate timestamps for all days like
Jan 1, 2015 00:00:00 - Jan 1, 2015 23:59:59
Jan 2, 2015 00:00:00 - Jan 2, 2015 23:59:59
Jan 3, 2015 00:00:00 - Jan 3, 2015 23:59:59
so on
I am able to do that. But I am getting some problem with day light saving issue. On march somewhere it is generating like this
Mar 8, 2015 00:00:00 - Mar 9, 2015 00:01:00 Which is wrong and it should be like Mar 8, 2015 00:00:00 - Mar 8, 2015 23:59:59
I found it because of day light saving issue. How to solve this issue ?
My code is:
public static List<String> getDatesRange(long start, long end, String tzOffset) {
//tzOffset is 420. for USA
TimeZone tz = TimeZone.getTimeZone(tzOffset);
List<String> dates=new ArrayList();
Calendar calendar = Calendar.getInstance(tz);
calendar.set(Calendar.DAY_OF_WEEK, 1);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
while (start<end) {
calendar.setTimeInMillis(start);
long startTime = calendar.getTimeInMillis();
int year= calendar.getWeekYear();
long endTime = start + (1 * 24 * 3600 * 1000L);
if(endTime<end) {
endTime-=1000;
System.out.println("Start Date= " + new Date(new Timestamp(start).getTime())+" ("+startTime+"), End Date= "+new Date(new Timestamp(endTime).getTime())+"("+endTime+")");
dates.add(startTime+"-"+endTime);
start= endTime+1000;
}
else{
System.out.println("Start Date= " + new Date(new Timestamp(start).getTime()) + " (" + startTime + "), End Date= " + new Date(new Timestamp(end).getTime()) + "(" + end + ")");
start=end;
dates.add(startTime+"-"+end);
}
}
return dates;
}
There are a couple things I wonder about this:
Why use Longs to define dates?
Why return a list of Strings instead of a list of Dates?
Why take the timezone into consideration at all in a case like this?
Nevertheless, you can achieve this simply using Calendar and SimpleDateFormat, like this:
public static Calendar getDayStart(final long timeInMillis) {
final Calendar cal = Calendar.getInstance();
// end time as a date
cal.setTimeInMillis(timeInMillis);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal;
}
public static List<String> getDatesRange(final long start, final long end) {
final Calendar cal = getDayStart(start);
final Date startDate = cal.getTime();
final Calendar calEnd = getDayStart(end);
//adding one day because of the strict comparison in the while below
calEnd.add(Calendar.DAY_OF_YEAR, 1);
final Date endDate = calEnd.getTime();
final SimpleDateFormat formatter = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
final List<String> dates = new ArrayList<String>();
final Date dayEnd;
String currentDay = "";
while(cal.getTime().before(endDate)) {
currentDay = formatter.format(cal.getTime());
currentDay += " - ";
//going to the end of the day
cal.add(Calendar.DAY_OF_YEAR, 1);
cal.add(Calendar.SECOND, -1);
currentDay += formatter.format(cal.getTime());
//going to next day again and continue the loop
cal.add(Calendar.SECOND, 1);
//add what we computed to the list of days
dates.add(currentDay);
}
return dates;
}
The problem is that you want to print dates without the notion of time zones (although the start dates depends on the tzOffset argument). If you use Java 8, the new Java time API has a class specifically designed for that: LocalDate.
So your method can be done by first determining the start and end day based on the timestamp and the time zone then get rid of all time zone considerations. And to print the range you can "cheat" by hardcoding the start/end time in the formatter.
public static List<String> getDatesRange(long start, long end, String tzOffsetMinutes) {
Instant startInstant = Instant.ofEpochMilli(start);
Instant endInstant = Instant.ofEpochMilli(end);
int offsetSeconds = Integer.parseInt(tzOffsetMinutes) * 60;
ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSeconds);
LocalDate startDate = OffsetDateTime.ofInstant(startInstant, offset).toLocalDate();
LocalDate endDate = OffsetDateTime.ofInstant(endInstant, offset).toLocalDate();
List<String> dates = new ArrayList<> ();
DateTimeFormatter fmt = DateTimeFormatter.ofPattern(
"MMM d, yyyy 00:00:00 - MMM d, yyyy 23:59:59");
for (LocalDate d = startDate; !d.isAfter(endDate); d = d.plusDays(1)) {
dates.add(fmt.format(d));
}
return dates;
}
You can probably do something similar with Calendars.
I need to get the month and day of today's date and offset dates. This is how I do it:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_WEEK, 0);
Date today = calendar.getTime();
System.out.println(today);
Output:
Wed Aug 27 15:07:35 CEST 2014
Two things, I need the month and the day to be numeric, like 8/27. I understand how to do that with today's date like so:
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
String a = String.valueOf(day);
String b = String.valueOf(month);
System.out.println(b +"/" + a);
My issue is that I might need to add an offset to that date, if I want tomorrows date for example. Is there a way to do that because converting Wed Aug 27.... to 8/27 would just be a pain. Thanks
Use simple date format:
Something like:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_WEEK, 1);
Date today = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
System.out.println(sdf.format(today));
DateFormat formatter = new SimpleDateFormat("MM/dd");
Calendar cal = Calendar.getInstance();
String calAsString = formatter.format(cal.getTime());
System.out.println(calAsString);
// Now for tomorrow's date:
int offset = 1;
cal.add(Calendar.DATE, offset);
calAsString = formatter.format(cal.getTime());
System.out.println(calAsString);
Use the Calendar to add a value to the day:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH,1)
I have two Jdatechooser(named as firstdate and lastdate) and Jspinner(named as starttime and endtime) in a gui.
The scenario is,
1.if i open gui i will get the current time and set it in endtime and currenttime-1 in starttime(the code is below),
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -1);
Date oneHourBack = cal.getTime();
String timeStamp = new SimpleDateFormat("HH:mm:ss").format(oneHourBack);
Date date = new SimpleDateFormat("HH:mm:ss").parse(timeStamp);
starttime.setValue(date);
2.For both the Jdatechooser i set the current date.
3.If current time is 00:44:36 (HH:mm:ss), in starttime(Jspinner) i have to set 23:44:36, with this i have to
set the firstdate(Jdatechooser) value to previous day date instead of current date.
for this am trying the following way,
Calendar currentTime = Calendar.getInstance();
Date curHr = currentTime.getTime();
String curtime = new SimpleDateFormat("HH").format(curHr);
int timeCheck = Integer.parseInt(curtime);
if(timeCheck > 00 && timeCheck < 01){
//code to set previous day's
date
}
is this the way to do it? or is there any better way available? Please help
You should be able to use the oneHourBack Date value as the value for the lastdate JDateChooser, as not only has the time been rolled back, but so has the date value, for example...
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 44);
cal.set(Calendar.SECOND, 36);
Date startTime = cal.getTime();
cal.add(Calendar.HOUR, -1);
Date endTime = cal.getTime();
System.out.println("startTime = " + startTime);
System.out.println("endTime = " + endTime);
Outputs...
startTime = Thu Feb 06 00:44:36 EST 2014
endTime = Wed Feb 05 23:44:36 EST 2014
This is the nice thing about Calendar
I want to add an event in android calendar from my application. I have added the event in calendar by using the following code.
Calendar c = Calendar.getInstance();
date2 = formatter.parse(eDate);
c.setTime(date2);
c.add(Calendar.HOUR, 1);
eDate = formatter.format(c.getTime());
date2 = formatter.parse(eDate);
date2 = formatter.parse(eDate);
c.setTime(date2);
eDate = formatter.format(c.getTime());
cal1=Calendar.getInstance();
cal1.setTime(date1);
cal2=Calendar.getInstance();
cal2.setTime(date2);
calEvent.put("dtstart", cal1.getTimeInMillis());
calEvent.put("dtend", cal2.getTimeInMillis());
String timeZone = TimeZone.getDefault().getID();
calEvent.put("eventTimezone", timeZone);
Uri uri = contentResolver.insert(Uri.parse("content://com.android.calendar/events"),
calEvent);
date2 = formatter.parse(eDate);
I need to add one hour to the calendar. So I have added 1 hour to the end date, using the code :
c.add(Calendar.HOUR, 1);
But when I look at in the calendar, it is showing 12 hour event. That means that if a added an event for 10 PM tomorrow, it creates an event from 10 PM tomorrow to 10AM tomorrow.
Can anybody tell me how to add an event that starts at 10 PM tomorrow and ends at 11 PM tomorrow?
You should check your format pattern which you have not shown to us.
More concrete, following code works for me:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd h a");
GregorianCalendar gcal = new GregorianCalendar();
gcal.set(Calendar.MILLISECOND, 0);
gcal.set(2013, Calendar.DECEMBER, 18, 22, 0, 0);
Date date1 = gcal.getTime();
System.out.println(formatter.format(date1)); // Output: 2013-12-18 10 PM
gcal.add(Calendar.HOUR, 1);
Date date2 = gcal.getTime();
System.out.println(formatter.format(date2)); // Output: 2013-12-18 11 PM
You can just add 1 hours (millisecond into your end time),
SomeThing like this
calEvent.put("dtend", calSet.getTimeInMillis() + 60 * 60 * 1000);
Basically, I want my result is what I expected that print 2012-10-23. However, It is very wired.
Here is my codes:
SimpleDateFormat ft = new SimpleDateFormat("YYYY-MM-dd");
Calendar cal = Calendar.getInstance();
cal.set(2012, 10, 22);
cal.add(Calendar.DATE, 1);
Date startDate = new Date();
startDate = cal.getTime();
String date = ft.format(startDate).toString();
System.out.println(date);
I want to print 2012-10-23, but the result is 2012-11-23.
Can someone tell me why it adds 1 month automatically? Thank you.
Calendar class months starts from 0, not from 1, so when setting month as 10 you're not setting October but November.
Change your code to
cal.set(2012, 9, 22);
Or even better
cal.set(2012, Calendar.OCTOBER, 22);
More info:
java.util.Calendar