Converting String to Timestamp, SimpleDateFormat, Omiting fields [duplicate] - java

This question already has answers here:
Convert String to java.util.Date
(4 answers)
Closed 7 years ago.
I read more questions on the web but I dont' find a solution yet.
I have a String like "14/05/1994", exactly in this format.
I need to covert it into java.util.Date in the same format.
I tried:
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dataFrom = new Date();
dataFrom = df.format("14/05/1994");
But the result is: Sat May 14 00:00:00 CET 1994
It's possibile have as a result: 14/05/1994 not as a String, but as java.util.Date?

A java.util.Date doesn't have a format. It's just a date.
When you print it out, e.g. using toString(), it uses a default format, which is what you're seeing. But you have that date.
Date dataFrom = new Date();
dataFrom = df.format("14/05/1994");
I don't think that can be your code because DateFormat.format accepts a Date and returns a String, not the other way around. You might mean df.parse, which would get you the results you describe. But if you take your SimpleDateFormat and use its format method on the Date, then you should get back out 14/05/1994 as you want. A java.util.Date doesn't have a format, though.

Related

Exact string to date and keep original format [duplicate]

This question already has answers here:
How to properly format the date?
(5 answers)
Get Date type object in format in java
(6 answers)
display Java.util.Date in a specific format
(11 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I have looked around for help on this, but again, it's just one of those things that I cannot find a suitable answer to my specific issue.
Here's 2 very detailed (and helpful) SO posts that I've looked at:
Change date format in a Java string
Java string to date conversion
This is what I have:
//Date Formatter
SimpleDateFormat dateFormatter1 = new SimpleDateFormat("yyyy-MM-dd", new Locale("EN"));
SimpleDateFormat dateFormatter2 = new SimpleDateFormat("dd MMMM yyyy", new Locale("EN"));
//Convert to Date
Date dateToParse = dateFormatter1.parse("2024-01-01");
//Format OUTPUT date
String dateAsString = dateFormatter1.format(dateToParse);
System.out.println(dateAsString); //01 January 2024
//Convert OUTPUT date from STRING to DATE
Date dateToReturn = dateFormatter1.parse(dateAsString);
System.out.println(dateToReturn.toString()); //Mon Jan 01 00:00:00 SAST 2024
Note:
I used both DateFormatter and SimpleDateFormatter but got the same output.
The outputs are very different and what I am trying to achieve is to have my String created as a Date object in the exact same format.
I feel I am missing something but I just cannot figure out what.
The code I provided is a snippet from a bigger function that returns type Date
The function wasn't created by myself, I'm picking up from where someone else left off
Whenever you call a .toString() method on a Date, then the DateFormatter is not taken into account. The format of date you get is just a matter of default toString implementation of Date class.
To use formatter while printing, try something like this (instead of the last line in your snippet):
System.out.println(dateFormatter1.format(dateToReturn));
Date.toString() returns always returns a string in the format dow mon dd hh:mm:ss zzz yyyy.
You'll need to reuse the formatter's format method to get the initial string you expect

Timestamp string to Date [duplicate]

This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
ISO 8601 String to Date/Time object in Android
(5 answers)
Closed 4 years ago.
How to convert the string to a date
In the string we have a timestamp (for example "2018-07-11T04:40:30Z"),
and I want to simply convert this String time to 11-07-2018 16:40:30
String Time = jresponse.getString("timestamp");
SimpleDateFormat sf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date date = sf.parse(Time);
I tried many times this code and it doesn't work because I have always this format "2018-07-11T04:40:30Z" and not this 11-07-2018 16:40:30 why ?
If you want to parse dates like "2018-07-11T04:40:30Z"then you should change the format as "dd-MM-yyyy HH:mm:ss" will never work with that date. The correct format should be something like yyyy-MM-dd'T'HH:mm:ssZ.

How to Parse Date in Java in Given Format? [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 5 years ago.
I try to use java.util.Date date = Date.from( Instant.parse(minDates)); to parse the date string given in format Wed Jan 17 2001 00:00:00 GMT 0530.
I am not able to figure out, how to do that in JAVA.
The want to convert the given date string in given format
2013-05-22T00:00:00
May be i am not able to figure it out, properly. If someone have way to do that suggest me in Java Only.
Here is the solution:
String dateToParse = "Wed Jan 17 2001 00:00:00 GMT 0530";
SimpleDateFormat in = new SimpleDateFormat("EEE MMM dd YYYY HH:mm:ss");
SimpleDateFormat out = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss");
Date date = in.parse( dateToParse );
System.out.println( out.format( date ) );
It will work if all dates are in the same timezone (GMT 0530)
Else it should be modified to support it, but I suppose you have the same timezone.
You can do that by using SimpleDateFormat 'parse' API.
You can initialize your SimpleDateFormat with any valid time format such as yyyy-MM-dd HH:mm:ss z and then parse any string which adheres to this format.
reff. to http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
One addition tip, use JodaTime as the Date and SDF in Java are getting deprecated: http://www.joda.org/joda-time/
If you are using Java 8+, You can use java.time.OffsetDateTime (or Instant...) instead of java.util.Date, which is incredibly easy.
OffsetDateTime odt = OffsetDateTime .parse("2013-05-22T00:00:00", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
Note that the second argument is optional in this case but you could have to specify one (with timezone id for example).
There is a solution without external which works with older version of Java and that manages timezones well. It consists of using JAXB's DataTypeConverter.
Date date = javax.xml.bind.DatatypeConverter.parseDateTime("2013-05-22T00:00:00+01:00").getTime();
Note that DatatypeConverter.parseDateTime returns a Calendar. You just need to call its getTime() method to convert is to a Date.

How to convert "2017-04-26T20:55:00.000Z" string to a Date in java android studio [duplicate]

This question already has answers here:
Java / convert ISO-8601 (2010-12-16T13:33:50.513852Z) to Date object
(4 answers)
Closed 5 years ago.
I am new to android studio and java development.
I would like to parse this date:
"2017-04-26T20:55:00.000Z"
, which I am getting from a hash map string.
I would like to display only the date on my view.
You can always use Java's DateFormat API for achieving this. Here is the code snippet that will help you to achieve whatever task you are looking for.
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
Date date = dateFormat.parse("2017-04-26T20:55:00.000Z");//You will get date object relative to server/client timezone wherever it is parsed
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); //If you need time just put specific format for time like 'HH:mm:ss'
String dateStr = formatter.format(date);
You will get date object from which you can use it whichever way you would like to display using date formatter to format again.
In my opinion the cleanest way to do it :
extract the date as described here : Java string to date conversion
create a string containing the formatted date you want thanks to another DateFormat
display it!
You could use substring like so
String date = yourString.substring(0, 10);
This would pull all the characters from 0 to 10 in your String and save it as a new String.
In this case that would return "2017-04-26"

How to change '2017-02-20 09:57:08.512534+00' format to "dd/MM/yyyy" in java? [duplicate]

This question already has answers here:
Parsing ISO-8601 DateTime with offset with colon in Java
(4 answers)
Closed 6 years ago.
Hi I have time string format : 2017-02-20 09:57:08.512534+00
How to change above format to "dd//MM/yyyy" format in java?
Convert String to date object using parse method
then format date object using format method as per your requirement
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd/MM/yyyy");
Date date = format1.parse("2017-02-20 09:57:08.512534+00");
System.out.println(format2.format(date));
Do yourself the favour of using the Java 8 java.time classes if you can use Java 8:
String dateTime = "2017-02-20 09:57:08.512534+00";
String date = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSx"))
.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
System.out.println(date);
This prints
20/02/2017
At face value it looks pretty much the same as the version using the now obsolete classes (Date and SimpleDateFormat). Still. For one thing, for your own good you will want to learn to use the new classes, not the legacy ones. Also, if some day you want to something else with the date than just convert from one string representation to another, the versatility and wealth of options of LocalDateTime and friends is likely to be useful.
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date = format.parse(myString);

Categories