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
Related
I am trying to parse a String that looks like:
2015, 2, 31, 17, 0, 1
so i figured I'll use
SimpleDateFormat("yyyy, MM, dd, hh, mm, ss")
but it assumed the months are 1-based. In this case the month (2) is March. How can i tell SimpleDateFormat or any other class to parse with zero-based months?
Use Calendar:
String[] yourString = "2015, 2, 31, 17, 0, 1".split(",");
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, Integer.valueOf(yourString[0]));
c.set(Calendar.MONTH, Integer.valueOf(yourString[1]));
c.set(Calendar.DAY_OF_MONTH, Integer.valueOf(yourString[2]));
c.set(Calendar.HOUR_OF_DAY, Integer.valueOf(yourString[3]));
c.set(Calendar.MINUTE, Integer.valueOf(yourString[4]));
c.set(Calendar.SECOND, Integer.valueOf(yourString[5]));
One solution i can see increase one month with
Date newDate = DateUtils.addMonths(new Date(), 1);
With calendar
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);
By default months are 0 based index. see
Why is January month 0 in Java Calendar?
Why are months off by one with Java SimpleDateFormat?
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
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);
My date object out put is 1.05 pm, and it should be 6.35 pm. How to convert gmt to gmt 5.30.
Please help!
Calendar cal = Calendar.getInstance(); // creates calendar
cal.setTime(new Date()); // sets calendar time/date=====> you can set your own date here
cal.add(Calendar.HOUR_OF_DAY, 5); // adds one hour
cal.add(Calendar.MINUTE, 30); // adds one Minute
cal.getTime(); // returns new date object, one hour in the future
=======> Setting time to calendar here
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());