I have tried the following; but the results are disappointing.
I want to increment the the months.
String dStartTime="2012-03-01";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-DD");
Date dateStartTime = dateFormatter.parse(dStartTime);
Calendar cal = Calendar.getInstance();
cal.setTime(dateStartTime);
cal.add(Calendar.MONTH, 1);
System.out.println(cal.getTime());
System.out.println(dateFormatter.format(cal.getTime()));
OUTPUT
Wed Feb 01 00:00:00 IST 2012 --- This is correct
2012-02-32 --- This is wrong. I want the Day should be one.
Please let me know what is the problem here?
Change new SimpleDateFormat("yyyy-MM-DD") to new SimpleDateFormat("yyyy-MM-dd").
DD is "Day in year" but you need dd "Day in month".
See http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html for Date and Time Patterns.
Related
Input date is 2016-01-01, but why output shows 2016/02/01?
String df = "2016-01-01";
String enddate="";
SimpleDateFormat DATE_FORMAT_QUERY = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
Calendar cal=Calendar.getInstance();
String[] dateStr=df.split("-");
int year=Integer.parseInt(dateStr[0]);
int month=Integer.parseInt(dateStr[1]);
int day=Integer.parseInt(dateStr[2]);
cal.set(year,month,day,23, 59,59);
System.out.println(cal.getTime());
enddate=DATE_FORMAT_QUERY.format(cal.getTime());
System.out.println(enddate);
Output:
Mon Feb 01 23:59:59 EST 2016 20160201T235959Z
ANSWER TO YOUR QUESTION
Input date is 2016-01-01, but why output shows 2016/02/01?
Because Calendar::month is 0-based.
month - the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.
You should use
int month=Integer.parseInt(dateStr[1] - 1);
CORRECT SOLUTION
NEVER parse manually a String containing a Date, better get date with SimpleDateFormat and use it to set Calendar time:
SimpleDateFormat dfo = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal=Calendar.getInstance();
cal.setTime(dfo.parse("2016-01-01"));
OUTPUT:
Fri Jan 01 00:00:00 CET 2016
20160101T000000Z
As an input I have Date object(for example, exDate=Fri Aug 01 00:00:00 EEST 2014) that must be formated. After the parsing of the date, I get wrong date.
SimpleDateFormat sdf = new SimpleDateFormat(
"dd-MMM-YYYY hh.mm.ss.SSSSSSSSS aa", Locale.ENGLISH);
String dateStart = sdf.format(exDate);
Date dateF = sdf.parse(dateStart);
dateStart will be equal to
01-Aug-2014 12.00.00.000000000 AM
and the resut, dateF will be equal to
Sun Dec 29 00:00:00 EET 2013
So, after the parsing of a string with date, the result is wrong.
Maybe, somebody know the source of the problem? Or another way to format date in another SimpleDateFormat?
The problem is the YYYY which means:
Y Week year;
The actual year, which is what you are looking for would be yyyy.
I really recommend that you go in the link above to see the full list.
You should also replace the milliseconds to .SSS as you can't get more precise than that.
I have some data which contains date in different formats, eg: yyyy-dd-MM, yyyy-MM-dd, EEE dd-MM-yy etc.
I am trying to find a way to differentiate between dd-MM-yyyy and MM-dd-yyyy.
I understand that if dd is less than 12, there is no way I can be sure about format, However by identifying other cases when dd > 12, I can minimize the the wrong calculation.
I tried this -
SimpleDateFormat targetFormat = new SimpleDateFormat("EEE, yyyy-MMM-dd hh:mm:ss a");
SimpleDateFormat originalFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat originalFormat2 = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
Calendar cal = Calendar.getInstance();
try {
Date date = originalFormat1.parse(s); //I tried with s = "2013-25-8 20:10:00";
cal.setTime(date);
if (cal.get(Calendar.DAY_OF_MONTH) > 12)
date = originalFormat2.parse(s);
System.out.println(targetFormat.format(date));
} catch (ParseException e) {
System.out.println("Error");
Output
I was expecting : Sun, 2013-Aug-25 08:10:00 PM
But I got : Thu, 2015-Jan-08 08:10:00 PM
You can try:
originalFormat1.setLenient(false);
before you try to parse a string with it; that should make it throw a ParseException when the month number is out of range.
When you apply format 1 and it interprets 25 as month then the date starts to become weird, that makes sense as month cannot be bigger than 12. Therefore, your if statement doesn't make sense. You have to check the format before applying the SimpleDateFormat (for instance with Integer.parseInt(s.substring(5,7) > 12).
Hey actually it is taking 25 as month, So 12 + 12 + 1 means 2 years and one month i.e "January"
So, your date becomes : "Thu Jan 08 20:10:00 GMT 2015" and again when you are changing it into "targetFormat" , this unusual result ensues...
Trying to compare some dates in java but can't get the formatting right, where am i going wrong?
DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
Date date1 = null, date2 = null, today = new Date();
date1 = (Date) df.parse(scan.next());
System.out.println(date1);
System.out.println(today);
if(date1.compareTo(today) < 0){
date1 = null;
System.out.println(start + " is not a valid date.. please try again!");
}
Please enter a start date:
10/04/2011
Mon Jan 10 00:04:00 GMT 2011
Tue Apr 05 22:27:44 BST 2011
I think you need MM, not mm
From the doc:
M Month in year
m Minute in hour
Change line 1 to be:
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
mm in SimpleDateFormat is the minutes. MM is the month. So your input is actaully January 10 2011 at 00:10:00
Check out http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html for abbreviations and javadoc.
I have two dates in Java:
Wed Jan 05 00:00:00 CET 2011
Sat Jan 15 23:59:59 CET 2011
Now I want to iterate over them, so that every day I can do a System.out.println() in which I put the date in this kind on the console:
2011-01-05
2011-01-06
2011-01-07
...
2011-01-13
2011-01-14
2011-01-15
How can I do this?
Best Regards, Tim.
Update:
Calendar calend = Calendar.getInstance();
calend.setTime(myObject.getBeginDate());
Calendar beginCalendar = new GregorianCalendar(calend.get(Calendar.YEAR), calend.get(Calendar.MONTH), calend.get(Calendar.DATE));
calend.setTime(myObject.getEndDate());
Calendar endCalendar = new GregorianCalendar(calend.get(Calendar.YEAR), calend.get(Calendar.MONTH), calend.get(Calendar.DATE));
while (beginCalendar.compareTo(endCalendar) <= 0) {
// ... calculations
beginCalendar.add(Calendar.DATE, 1);
}
Use the GregorianCalendar object to increment one day at a time
Output using SimpleDateFormat.
To get your date from a string, into a Date object, you have to do the following
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = format.parse(yourDateString);
Then, you need to convert into a GregorianCalendar, so that you can easily increment the values and finally output the date using another SimplerDateFormat in the way you want to. See the documentation for the different codes.
Update:
Update, following your code update, you can simply do the following
Calendar beginCalendar = Calendar.getInstance();
beginCalendar.setTime(myObject.getBeginDate());
Calendar endCalendar = Calendar.getInstance();
beginCalendar.setTime(myObject.getEndDate());
while (beginCalendar.compareTo(endCalendar) <= 0) {
// ... calculations
beginCalendar.add(Calendar.DATE, 1);
}
Create a Calendar object and set it at the start date. Keep adding a day at a time and printing until you're at the end date.