Compare date Java format problem - java

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.

Related

Negative Timestamp Java

I have wierdo problem with timestamp in Java/Android
Date inputDate = null;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Log.e("DateH:D", hour+" "+day);
try {
inputDate = sdf.parse(hour + " " + day);
Date currentDate = new Date();
Log.e("InputDate", inputDate.toString());
Log.e("InputDate",inputDate.getTime()+"");
Log.e("CurrentDate", currentDate.toString());
Log.e("CurrentDate",currentDate.getTime()+"");
if (!inputDate.after(currentDate) ){
//TODO change this string
hourField.setError("Date from past");
return false;
}
} catch (ParseException e) {
Log.e("DateParser" , e.getLocalizedMessage);
return false;
}
Example output for this is:
DateH:D: 12:33 15/09/16
E/InputDate: Tue Sep 15 14:33:00 CET 16
E/InputDate: -61640134020000
E/InputDate: Tue Sep 15 14:33:00 CET 16
E/CurrentDate: Thu Sep 15 11:38:43 CEST 2016
E/CurrentDate: 1473932323198
So non-timestamp representation of date is correct but timestamp is wrong. How it's possible? What i'm doing wrong?
Use yy to parse 2-digit years.
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yy");
The getTime method of Date:
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
represented by this Date object.
Since the year you specified is year 16, the negative result makes sense.
The starting date for epoch milis is Thu, 01 Jan 1970 00:00:00 GMT. Any datetime before that is negative in epoch milis.

Java DateTime convert

I'm converting a specific datetime fromat by deducting -5 minutes from the current date I retrieve:
csvFileDate is a list of dates with format, yyyyMMddhhmm. Below is the code I'm using but its converting the date wrong:
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmm");
private List<String> csvFileDates = new ArrayList<String>();
....
Collections.sort(csvFileDates);
String currDate = csvFileDates.get(csvFileDates.size() -1);
System.out.println("DATE1 " + currDate);
Calendar c = Calendar.getInstance();
c.setTime(dateFormat.parse(currDate));
c.add(Calendar.MINUTE, -5);
System.out.println("DATE2 " + c.getTime());
output:
DATE1 201505181208
DATE2 Mon May 18 00:03:00 SGT 2015
Another:
DATE1 201505181213
DATE2 Mon May 18 00:08:00 SGT 2015
Any idea why?
You have 24 hours time format. So you have to use uppercase H for hours:
H Hour in day (0-23)
h Hour in am/pm (1-12)
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
See the documantation of SimpleDateFormat for more details.
Use HH instead of hh for hour of the day, as stated here

Invalid date value after parse

I am new to java. I have a Date that is stored in the variable, pubDate = "2013-09-23"
When I'm executing this
SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
Date publishDate = pubSimpleDateFormat.parse(pubDate);
I'm getting wrong value : Wed Jan 23 00:09:00 GMT+05:30 2013
Please help me why it so. and help me to solve this.
M is for Month in year while m is for Minute in hour
You should use SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String pubDate = "2013-09-23";
SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date publishDate = pubSimpleDateFormat.parse(pubDate);
System.out.println(publishDate);
Output :
Mon Sep 23 00:00:00 GMT 2013
Read the section Date and Time Patterns.

not getting proper date while parsing String date to Date

DateFormat formatter = new SimpleDateFormat("yy-mm-dd");
formatter.setLenient(false);
String[] dateStr = { "2013-12-27", "2013-01-03"};
for (int i = 0; i <= 1; i++) {
Date date = formatter.parse(dateStr[i]);
System.out.println("date is "+date);
}
result :
Sun Jan 27 00:12:00 IST 2013
Thu Jan 03 00:01:00 IST 2013
i am parsing string date in to Date.but it is giving me date Starting with month Jan regardless of what month i am passing to formatter constructor.
The format for your date would be yy-MM-dd. Update your format and check.
mm for minutes
MM for month
Use: "yy-MM-dd"
See here
once Silly Mistake
DateFormat formatter = new SimpleDateFormat("yy-MM-dd");
Format this line in your code

java.util.date/Calendar/SimpleDateFormat: Adding/Incrementing the months in java

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.

Categories