Troubleshooting Java DateFormat Month Parsing Issue - java

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"

Related

DateTime addmonths method is adding 1 extra day

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

SimpleDateFormat does not give correct date [duplicate]

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

output need to be in date format

is this possible to get the output in Date type in the below format
> 2014-11-12 09:23:47 GMT+05:30
not to be like
> Wed Nov 12 06:53:47 IST 2014
That can be done using SimpleDateFormat with the format string:
yyyy-MM-dd HH:mm:ss 'GMT'XXX
as per the following program:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date dt1 = new Date();
DateFormat df = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss 'GMT'XXX");
String line = df.format(dt1);
System.out.println(line);
}
}
On my system, that gives me:
2014-11-14 15:36:16 GMT+08:00

Mysql Date Format in Java

I have a website which supplies date in 2 formats: 28th June 2009 or June 2009.
Now I would like to convert both of these into the same format yyyy-mm-dd hh:mm:ss using MySQL and Java.
SimpleDateFormat gives an error: "Unparsable Date". What's the solution?
What about June 2009 as you can not say its a date you need to make it a date by adding a day in this month-year format. Ex.. add first day of month here and make it 1 June 2009 then parse it in desired format.
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) throws IOException, ParseException
{
String dateStr = "28 June 2009";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(sdf.format(new Date(dateStr)));
}
}

java string to datetime conversion issue

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.

Categories

Resources