How to convert date string to Date [duplicate] - java

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 9 years ago.
I have the following string: Feb 16, 2014 2:11:41 PM
I have Googled around for some time now and only see examples on converting strings in the format mmddyyyy. I can't seem to find out how to convert the format above.
How can I convert this to a Date.
Thanks,
Gary

Use below formatter:
SimpleDateFormat formatter = new SimpleDateFormat("EEE dd,yyyy HH:mm:ss aa");
Also this link would be very helpful

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

Converting the timestamp from Facebook data after downloading messages in JSON format in Java [duplicate]

This question already has answers here:
How to convert currentTimeMillis to a date in Java?
(13 answers)
Convert String in milliseconds to Date Object (JAVA) [duplicate]
(4 answers)
Converting Epoch time to date string
(10 answers)
Closed 4 years ago.
I was unable to find an answer to this problem, so once I figured it out, I felt posting the answer might help other people down the road.
The issue is when you have downloaded your messages from Facebook and you download them in JSON format, the timecode needs to be converted to a format that can be used in your Java program.
The timecode from Facebook is a 13 digit number that looks something like this: 1548410106047
The solution to the problem is:
You must first convert it to a Long datatype, then simply make a Timestamp out of it like this:
Long fbt = Long.parseLong(facebookTime);
Timestamp ts = new Timestamp(fbt);
Once you have it in a timestamp you can do anything with it such as:
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM dd, YYYY hh:mm a");
System.out.println(sdf.format(ts));
The output from above looks like this:
Friday, January 25, 2019 01:55 AM

Specific DateFormat in java [duplicate]

This question already has answers here:
Parsing a date’s ordinal indicator ( st, nd, rd, th ) in a date-time string
(5 answers)
Closed 4 years ago.
I have a date of the form "20th Oct 2017" how do I convert it to "2017-10-20". In specific I don't see the format for 20th. I am able to get the format for month and year "MMM yyyy" I would like to know the format for the day.
Try this one:
DateTimeFormatter parseFormatter = DateTimeFormatter.ofPattern("MMM d['st']['nd']['rd']['th'] uuuu h:mma", Locale.ENGLISH);
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, parseFormatter);
Considering Java 8 features, you can format your date in various ways.
Just check the following formats and ways of parsing and formatting the date:
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

java SimpleDateFormat giving wrong result [duplicate]

This question already has answers here:
Java SimpleDateFormat returns unexpected result
(3 answers)
Closed 6 years ago.
I am using java.text.SimpleDateFormat in Scala to convert string to date.
val isdf = new SimpleDateFormat("dd/MM/yyyy")
isdf.parse("01/22/2016") gives Sun Oct 01 00:00:00 UTC 2017
How to fix this ? Are there any alternative?
Your problem is that you have "22" in the month position. The date format you set is day/month/year, but it looks like the date you sent is in the format month/day/year.
The formatting is different between the dates. You have mixed the month and Day up in your statements. try this:
val isdf = new SimpleDateFormat("MM/dd/yyyy");
goodluck with the code.

Java: how to handle a date with format like '2014-01-01T01:14:48.000+08:00' [duplicate]

This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 7 years ago.
I got a date 2014-01-01T01:14:48.000+08:00 from a web server. Does anyone know how to parse this correctly? How to get its time offset?
String s="2014-01-01T01:14:48.000+08:00";
SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
System.out.println(sdf.format(s));
You need something like this,
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSX");
System.out.println(sdf.parse(y));
The X is for timezone.

Categories