String to date not working properly [duplicate] - java

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 8 years ago.
I am trying from half an hour to convert string to date by using following code:
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
Date lastCharged = dateFormat.parse(lastChargeDate);
Every time I run this code the date returned by the system is Sun Dec 29 00:00:00 PKT 2013 Even if i changed the date manually same is the response by the system.
Any help in this regard a lot of work is suspended just because of this blunder.

DateFormat#parse() method just convert the String to Date. It doesn't change anything in the converted Date it means it doesn't store the format from which it is constructed.
Whenever you print the Date object again then it prints in its default toString() implementation that's what you are getting.
It you need to print it again in specific format then use DateFormat#format() method.
The format should be yyyy-MM-dd instead of YYYY-MM-dd.
Sample code:
String oldDate="2014-06-07";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date=dateFormat.parse(oldDate);
System.out.println(date);
String newDate=dateFormat.format(date);
System.out.println(newDate);
output:
Sat Jun 07 00:00:00 IST 2014
2014-06-07

Related

Cant display date as yyyy-MM-dd'T'HH:mm:ss:SSS Java [duplicate]

This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Closed 2 years ago.
Im getting the following error when I try to convert the following string. Id like the Date to be in the format yyyy-MM-dd'T'HH:mm:ss:SSS but instead the Date seems to be coming out as Sun Mar 01 23:00:01 GMT 2020
String FULL_ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
SimpleDateFormat formatter = new SimpleDateFormat(FULL_ISO_DATE_FORMAT);
Date from = formatter.parse("2020-03-01T23:00:01.000");
Error
feign.FeignException: status 400 reading Controller#searchController(Date,Date,Integer,String); content:
{"status":"fail","data":{"errors":[{"type":"IllegalArgumentException","description":"Invalid value Sun Mar 01 23:00:01 GMT 2020 for filter from. Field needs to be in format: yyyy-MM-dd'T'HH:mm:ss.SSS"}]}]}
Any help would be appreciated. I need to use the Date object as the constructor Im querying is using the Date object. Ideally I'd like to use LocalDateTime but I cant.
Use the LocalDateTime from java-8 date-time API and stop using legacy Date classes
String FULL_ISO_DATE_FORMAT = DateTimeFormatter. ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime dateTime = LocalDateTime.now();
dateTime.format(FULL_ISO_DATE_FORMAT);
Please don't use the old classes Date and SimpleDateFormat. Use the new java.time api that is much more robust and better designed.
You can do the same thing as follows:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse("2020-03-01T23:00:01.000", formatter);
Keep in mind that you can convert it to Date for compatibility like so:
Date legacyDate = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());

Creating date in java with SimpleDateFormat [duplicate]

This question already has answers here:
How to convert a String to a Date using SimpleDateFormat?
(10 answers)
Closed 7 years ago.
Update:
Really?!!! Duplicate??? My format is correct (yyyy/MM/dd HH:mm:ss) but return time is incorrect. How this is similar to another question????
I'm trying to create java Date but it's always return wrong value. This is my code:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:MM:SS");
Date GreDate = dateFormat.parse("2014/03/22 00:00:00");
And GreDate return Sun Dec 22 00:00:00 GMT+03:30 2013 as value.
Please don't suggest to use external library for date type.
Update:
I changed my pattern to this:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Now GreDate returns Sat Mar 22 01:00:00 GMT+04:30 2014. Year is correct but time still not 00:00:00.
Note that:
MM are the months, mm are the minutes.
SS are the milliseconds, ss are the seconds.
So you need to change your dateFormat to
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Basically there are two errors in your pattern, both in the time part (seconds and minutes).
Here is the link to the complete documentation link.

Convert date format not working

hello guys I've been on this issue for 4 hours and I don't seem able to solve it.
I want the date selected using JDateChooser to be converted to this format dd-mm-yyyy but I couldn't do this.
This what I wrote but it prints the date as :Thu Aug 21 00:00:00 WEST 2014. I want it to be printed 21-08-2014
This is my code:
public void addIns() {
JTextField ch=(JTextField) dateChooser.getDateEditor().getUiComponent();
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
try{
Date date = df.parse(ch.getText());
} catch(Exception e){System.out.println("wrong date");}
System.out.println(date); }
The format Thu Aug 21 00:00:00 WEST 2014 is the default format of the Date.toString method.
So, if you are expecting:
System.out.println(date);
To print 21-08-2014, then you are wrong.
You could:
in Java 8 use LocalDate and LocalTime, and have a decent toString (but that won't print 21-08-2014).
print the date with the format method, eg: df.format(date);
And, since you are using Swing - and Swing components - there is probably a better way to get or set the format on the component.

Can't make java.text.SimpleDateFormat to work

I have a Date object as follows:
java.util.Date d = new java.util.Date(System.currentTimeMillis()); // Mon Dec 23 14:57:28 PST 2013
I need to format the date to get another Date object with this format instead:
2013-12-23 14:57:28
I tried this code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
sdf.format(d); // d is still Mon Dec 23 14:57:28 PST 2013, no formatting.
I tried this code:
String s = d.toString();
try {
d = sdf.parse(s);
} catch (Exception e)
e.printStackTrace(); // java.text.ParseException: Unparseable date: "Mon Dec 23 14:35:48 PST 2013"
Would you please tell me what am I doing wrong? I googled searched it but the solutions to format a Date was more or less what I tried. Any help is greatly appreciated.
You don't understand what a Date is, and what format() does. A Date is just a number of milliseconds. Nothing more. It doesn't have any format. Formatting a date doesn't change the date at all. It returns a string containing a human readable representation of the date (like "2012-11-23" or "Monday, April 2").
So, the following instruction:
sdf.format(d);
is effectively a noop. You ignore the string that it returns.
If what you want is to have a specific format used when calling date.toString(), it's impossible. When you want to display a date in a specific format (yyyy-MM-dd for example), instead of doing
System.out.println(date);
use
DateFormat format = new SimpleDaeFormat("yyyy-MM-dd");
System.out.println(format.format(date));
All this is clearly explained in the javadoc. You should read it.
SimpleDateFormat, does not change the date format, it gives you a formatted date for display purpose only, not for anything else.
util.Date will always have one format (a long number of milliseconds) that you can format to any way you want in order to display using SimpleDateFormat. So in effect no matter what date you get you can format to what format you want.
If you explain why you are trying to do what you are trying to do, then maybe we can support you better.

What is the proper date format for a given date

What is the proper date format for this date instance ..
10/10/2011 2:36:00 PM
I've used this ..
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a");
edit
more code
Object temp = jsonArray.getJSONObject(i).get("STARTDATE"); // date object from webserive
Date appointment.mStartDate = formatter.parse(temp.toString());
but it returned this date in this format ..
Thu Nov 10 00:36:00 GMT+02:00 2011
but it returned this date in this format ..
Thu Nov 10 00:36:00 GMT+02:00 2011
You were thus doing System.out.println(appointment.mStartDate);? That's then perfectly fine. It's indeed the default format of the Date#toString() method. When you pass a non-String object to System.out.println(), then its toString() method will be called and the returned String will be displayed.
If you want to display it in the same format as you have retrieved it, then you should be using the SimpleDateFormat#format() method to convert Date to a String in the desired format:
String dateString = formatter.format(appointment.mStartDate);
System.out.println(dateString);
Use parseObject
Date appointment.mStartDate = (Date) formatter.parseObject(temp.toString());
There is four acknowledged variants:
US M/D/YY
ISO-8601 YYYY-MM-DD
JIS ?
EUR DD.MM.YYYY
What's your problem? The date appears to have been parsed correctly for a timezone of +2 hours. Naturally, when you simply print the toString description, the displayed value is in the default format -- to format the output you need to do a date formatter operation.

Categories