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
Related
This question already has answers here:
Creating java date object from year,month,day
(6 answers)
Why is January month 0 in Java Calendar?
(18 answers)
Closed 10 months ago.
Trying to convert ist into cst but I'm not getting the expected output if, I passes the hardcoded value. Expected output is>
IST:: 18-04-2022 14:11:53
CST:: 18-04-2022 03:41:53
and output I am getting after hardcode value (one month ahead)
IST:: 18-05-2022 14:10:33
CST:: 18-05-2022 03:40:33
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class datetime {
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
Calendar Local = Calendar.getInstance();
Local.set(2022, 4, 18, 14, 10);
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String ist = formatter.format(Local.getTime());
System.out.println("IST:: " + ist);
Date theist = formatter.parse(ist);
//Convertion of ist into cst
TimeZone cst = TimeZone.getTimeZone("CST");
formatter.setTimeZone(cst);
String cst1 = formatter.format(theist.getTime());
System.out.println("CST:: "+ cst1);
}
}
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 5 years ago.
How to parse the following timestamp in string formate to date formate.
Mon Sep 25 13:40:56 GMT+05:30 2017
Since it has GMT in the timestamp so I didn't find a valid answer for this. Please let me know how to write SimpleDateFormat for this?
in java 7 with SimpleDateFormat
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
String input = "Mon Sep 25 13:40:56 GMT+05:30 2017";
String dateFormat = "EEE MMM d HH:mm:ss z yyyy";
try {
Date date = new SimpleDateFormat(dateFormat).parse(input);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
See it in action : https://ideone.com/RaCugz
This question already has answers here:
Convert Date to String using simpledateformat
(5 answers)
Closed 6 years ago.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
String dateInString = "2016-04-23";
try {
Date date = formatter.parse(dateInString);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
System.out.println("Date: -- "+date);
System.out.println("FormatterDate : -- "+formatter.format(date));
System.out.println("SQLDATE : -- "+sqlDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Output :
Date: -- Sat Jan 23 00:04:00 CST 2016
FormatterDate : -- 2016-04-23
SQLDATE : -- 2016-01-23
My out put should be 2016-04-23 not 2016-01-23
As per the javadocs
m Minute in hour
You want
M Month in year
Kindly note that mm stands for Minute and not Month. For month you have to use MM instead.
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"
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.