I can't seem to see the problem with the example code below. For some reason seems to be ignoring the year and saying the dates are the same, as can be seen in the output below. I must be missing something simple.
01/28/2006
01/16/2007
Tue Apr 01 00:00:00 PDT 2008
Tue Apr 01 00:00:00 PDT 2008
done
import java.util.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
class ExampleProgram {
public static void main(String[] args){
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String d1String = "01/28/2006";
String d2String = "01/16/2007";
System.out.println(d1String);
System.out.println(d2String);
Date d1=null;
try {
d1 = df.parse(d1String);
} catch (ParseException e) {
System.out.println(e.getMessage());
}
Date d2=null;
try {
d2 = df.parse(d2String);
} catch (ParseException e) {
System.out.println(e.getMessage());
}
System.out.println(d1);
System.out.println(d2);
System.out.println("done");
}
}
"dd/MM/yyyy"
should read:
"MM/dd/yyyy"
As Peter mentioned, the meaning of the letters can be found in the documentation here:
https://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html.
The reason that it wasn't giving you what you expected is like Peter said the SimpleDateFormat should read "MM/dd/yyyy"
The reason that the result is saying that they appear to be equal is because with the format that you've given it "dd/MM/yyyy", d1String's Month is 28. It is taking 28 - 12, adding a year, 16 - 12, adding another year, and the result is 4 (April) and the year is now 2008. Same thing for d2String.
You could try declaring your dates as Date objects.
Related
I am currently working on an issue where DateTime.addMonths(iStartDateH, durationAsInt) is adding an extra day. It uses GeorgianCalendar internally.
We are using Java 5 currently in this project
Eg: For 24 months
ExpirationDate=DateTime.addMonths(currentDate, 24)
CurrentDate= 01/02/2021 (dd/mm/yyyy format)
ExpirationDate= 02/02/2023
public static ErrorCode addMonths(DateHolder dateH, int numMonths) {
try {
Calendar c = new GregorianCalendar();
c.setTime(dateH.value);
c.add(Calendar.MONTH, numMonths);
dateH.value = c.getTime();
return ErrorCode.successCN;
}
catch (Exception e) {
IlMessage msg = new IlMessage(Msg.exceptionCaughtCN, e);
IlSession.getSession().getMessageStack().push(msg);
return ErrorCode.errorCN;
}
}
I tried checking the complete date/time difference and its coming as 730.773935185185185
Please help with the same.
I am using Java 8 and I tried the code below and it worked just fine for me (for testing purposes I set the date as Feb 1 as from your example.
public static void main(String...pStrings) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate currentDate = LocalDate.of(2021, 2, 1); //LocalDate.now();
System.out.println("Original Date -" +currentDate.format(formatter));
LocalDate newDate = currentDate.plusMonths(24);
System.out.println("updated date - " + newDate.format(formatter));
}
I received the output: -
Original Date -01/02/2021
updated date - 01/02/2023
Note that m is for minutes. For a month, you need to use M.
The implementation of your class, DateHolder seems to have a problem. There is no such problem with java.util date-time API for this requirement.
Demo:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws ParseException {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).parse("01/02/2021"));
System.out.println(calendar.getTime());
int numMonths = 24;
calendar.add(Calendar.MONTH, numMonths);
System.out.println(calendar.getTime());
}
}
Output:
Mon Feb 01 00:00:00 GMT 2021
Wed Feb 01 00:00:00 GMT 2023
This question already has answers here:
String to Date conversion returning wrong value
(5 answers)
Closed 5 years ago.
I am trying to find the difference between two dates and I did the following:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class test {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD");
String accessioned = "2017-04-27";
System.out.println(date);
try {
date = format.parse(accessioned);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(date);
Date now = new Date();
long diff = now.getTime() - date.getTime();
System.out.println(diff);
if ((diff / (1000 * 60 * 60 * 24)) >= 30) {
System.out.println("haha");
}
}
}
This is the output I get:
Fri Jul 21 14:23:59 CEST 2017
Fri Jan 27 00:00:00 CET 2017
15168239705
haha
The Problem is if I change the the String accessioned for e.g to "2017-04-28" the date changes accordingly, same thing for year but whatever value I put for month, It always outputs January. For e.g in my code it should be April but the output says Jan.
What am I doing wrong?
Change D to d (SimpleDateFormat Doc):
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
D is for day in year (1-365)
d is for day in month (1-31)
Also you can now use LocalDate from Java8 more convenient to use DateTimeFormatter Doc / LocalDate Doc
You apparently want to check if the difference of days is >=30 between your date and now or not, so I'll propose another solution with LocalDate :
LocalDate localDate = LocalDate.parse("2017-04-27", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
long daysDiff = localDate.until(LocalDate.now(), ChronoUnit.DAYS);
System.out.println(daysDiff);
if (daysDiff >= 30){
System.out.println("haha");
}
EDIT : You need to use LocalDateTime instead of LocalDate to be able to use hour/minute/sec also
LocalDateTime localDateTime = LocalDateTime.parse("2017-04-21T11:51:36Z", DateTimeFormatter.ISO_DATE_TIME);
The problem is your date format: new SimpleDateFormat("yyyy-MM-DD")
As listed here, M is already a two-digit format - and D stands for "Day in year", not "Day in month", which is d.
Your format should look like this:
SimpleDateFormat("y-M-d")
...which gives me:
Fri Jul 21 14:44:03 CEST 2017
Thu Apr 27 00:00:00 CEST 2017
7397043764
haha
This question already has answers here:
Java Date year calculation is off by year for two days
(5 answers)
Closed 5 years ago.
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class myTests {
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("YYYY-MMM-dd", Locale.US);
try {
Date sortingDate = (Date)formatter.parse("2017-Jul-13");
System.out.println("Sorted Date is:"+sortingDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Result is
Sorted Date is:Sun Jan 01 00:00:00 PST 2017
Why wont it show date i gave 2017 Jul 13
Can you please let me know.
Thanks
Abe
uppercase Y is Week year. What you Need is lowercase y = Year.
So Change new SimpleDateFormat("YYYY-MMM-dd", Locale.US); to new SimpleDateFormat("yyyy-MMM-dd", Locale.US); and you should get the correct result.
For more informations see the javadoc of SimpleDateFormat
If you are using java8, you should Change to DateTimeFormatter and the new DateTime API
I'm trying to get the following bit of code to work with both input Strings. It works for the one without the dash (this is a limitation of SimpleDateFormat). I get the ParseException for the second input. JodaTime has been a suggestion in related posts, however, I can't use that and I'm on Java 6. I'd like to avoid regex solutions, though, a simple one for the format I have that works for both Strings would do. I'm going to drive home now, and check this out again in a couple of hours, with updates on what I find.
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class testcc {
public static void main(String[] args) {
String input = "Wed Jul 02 00:00:00 EDT 2014";
// String input = "Wed Jul 02 00:00:00 GMT-400 2014";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
Date date = null;
try {
date = df.parse(input);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
DateFormat dfTarget = new SimpleDateFormat("dd-MMM-yyy");
System.out.println(dfTarget.format(date));
}
}
It is likely the case that the source of the date String (a GWT extension widget) is returning it incorrectly within the context of SimpleDateFormat. That is, since z expects a colon (GMT-4:00), not having that fails. I ended up with something silly like this:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class testcc {
public static void main(String[] args) {
// String input = "Wed Jul 02 00:00:00 PST 2014";
String input = "Wed Jul 02 12:00:00 GMT-400 2014";
int pos = input.indexOf('-');
if (pos == -1)
pos = input.indexOf('+');
if (pos != -1) {
input = input.substring(0, pos + 2) + ":"
+ input.substring(pos + 2, input.length());
}
DateFormat df = new SimpleDateFormat("EEE MMM d kk:mm:ss z yyyy", Locale.ENGLISH);
Date date = null;
try {
date = df.parse(input);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
DateFormat dfTarget = new SimpleDateFormat("dd-MMM-yyy");
System.out.println(dfTarget.format(date));
}
}
Are there any cases that I missed given the String will always either be: "Wed Jul 02 12:00:00 GMT-400 2014" or "Wed Jul 02 12:00:00 EST 2014" or "Wed Jul 02 12:00:00 PST 2014", and the likes?
I am running into some odd output using the Java, DateFormat object. For some reason it is adding one to my month and I am not sure why. I have broken down the problem as simple as possible.
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* #author djc39_000
*/
public class TestDate {
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
Date testDate;
try {
testDate = (Date) formatter.parse("12/6/2013 11:23:04 AM");
System.out.println(testDate);
} catch (ParseException ex) {
System.out.println(ex);
}
}
}
Output:
Sun Jan 06 11:23:04 EST 2013
Expecting Output:
Fri Dec 06 11:23:04 EST 2013
Also, if I change the month to 11 in my string it does not change the month in the stamp. What am I doing wrong? Thanks
Solution was found, mm is for mins, and I used for month which should have been MM.
Are there any other characters that might be easily confused for bonus points?
Date format should be "MM/dd/yyyy hh:mm:ss a"