This question already has answers here:
How can I convert a date in YYYYMMDDHHMMSS format to epoch or unix time?
(2 answers)
Changing String date format
(4 answers)
Change date format in a Java string
(22 answers)
Closed 3 years ago.
I am currently facing a problem when it comes to some Java methods that weren't explained to me in lectures very well. I need to write a program that accepts user-inputted strings (particularly a date) in yyyymmddhhss format, which should then convert to hh:mm Month day, year.
E.g. 201901151500 outputs: "03:00 PM January 15, 2019".
Currently, in my program, I have accepted the user's input and implemented a method that returns an error message if the inputted format is invalid.
Any tips on where to go from here? Advice is greatly appreciated-- thank you!
If you are using Java 8 you can use java.time API like so :
String input = "201901151500";
LocalDateTime dt = LocalDateTime.parse(input, DateTimeFormatter.ofPattern("uuuuMMddHHmm"));
String output = dt.format(DateTimeFormatter.ofPattern("hh:mm a MMMM dd, uuuu"));
>> output = 03:00 PM janvier 15, 2019
Use the DateTimeFormatter as defined here: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
Pay attention to the parse method. You can define a formatter that takes in a string and then returns it in a certain way, almost any way you choose.
Here is the LocalDataTime class:
https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
Example code:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
LocalDateTime local = LocalDateTime.parse("2004 12 25", formatter);
Related
This question already has answers here:
Convert String to Date format in andorid
(2 answers)
Closed 1 year ago.
I'm trying to convert timestamps to date, I got this exception:
java.text.ParseException: Unparseable date: "1604328483716"
at java.base/java.text.DateFormat.parse(DateFormat.java:395)
All the timestamps values that I have, having a format like this 1604328483716
Your formatter is set up to handle the format "EEE, d MMM yyyy HH:mm:ss Z". "1604328483716" isn't remotely in that format.
The value "1604328483716" looks like the string version of a milliseconds-since-The-Epoch value. If so, convert it to a long (Long.parseLong) and use new Date(theLongValue), which will give you a Date instance for Monday November 2nd 2020 14:48:03 GMT (or whatever that is in your local timezone).
You might also consider using the newer date/time API in the java.time package, rather than java.util.Date.
1604328483716
It is timestamp: https://www.unixtimestamp.com/?ref=dtf.ru
So, simply do:
long modificationTime = rec.getJsonNumber("modificationTime").lngValue();
Date date = new Date(modificationTime);
This question already has answers here:
Why does Java's java.time.format.DateTimeFormatter#format(LocalDateTime) add a year?
(2 answers)
Using DateTimeFormatter on january first cause an invalid year value
(1 answer)
Closed 3 years ago.
I am trying to convert a date of the format 2019-12-30 to a date with format 30-12-2019 and for this I thought of using DateTimeFormatter and I have the following code for this:
LocalDate date = LocalDate.parse("2019-12-30");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-YYYY");
System.out.println(formatter.format(date));
However, to my surprise, this returns an output:
30-12-2020
instead of the expected 30-12-2019. If the date is set to 2019-11-30, it correctly returns 30-11-2019. I am doing something terribly wrong but I am not able to figure out what exactly. Can someone please help?
From the DateTimeFormatter documentation:, indicating the symbol, meaning and examples:
Y week-based-year year 1996; 96
So you're formatting the week-based-year, not the regular year. December 30th 2019 belongs to the first week of 2020, hence your output.
Use yyyy (year-of-era) or uuuu (year) instead of YYYY and you'll get 2019 instead.
Basically, YYYY should usually be used with w (week-of-week-based-year) and E (day-of-week).
Use "dd-MM-yyyy" instead of "dd-MM-YYYY"
Try the following code snippet to get expected result -
LocalDate date = LocalDate.parse("2019-12-30");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
System.out.println("date " + formatter.format(date)); // date 30-12-2019
You are using DateTimeFormatter.ofPattern("dd-MM-YYYY"); and notice that Y stands for week-based-year, which you probably don't want. Perhaps you want yout pattern to be "dd-MM-uuuu". For more information on patterns, refer to the official Oracle documentation: Patterns for Formatting and Parsing
This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Change the format of Date Java [closed]
(2 answers)
Closed 4 years ago.
I have the following piece of code:
System.out.println(array[9]);
Date d = df.parse(array[9]);
System.out.println(d.toString());
and the result of this looks like the following:
01.01.2017
Sun Jan 01 00:00:00 CET 2017
My DateFormatter:
DateFormat df = new SimpleDateFormat("dd.MM.yyyy",Locale.GERMANY);
So my question is now why I get the wrong format.
First result is a string, which I must convert to date.
But I got the wrong format, not the German one (dd.MM.yyyy).
What's wrong?
In your example you should use df.format(d) if you plan to convert Date to String. The default Date.toString() method will use the predefined format which you can't control.
This question already has answers here:
Java string to date conversion
(17 answers)
Calendar date to yyyy-MM-dd format in java
(11 answers)
java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
(8 answers)
Closed 4 years ago.
I have a string e.g. Thu May 10 15:48:23 IST 2018. How to convert this string in the form of Calendar object with format 2018-05-10 15:48:23.84.
Start by using having a look at DateTimeFormatter and Parsing and Formatting for more details about how to parse and format date/time values in Java 8+
Based on your examples, something like...
String inValue = "Thu May 10 15:48:23 IST 2018";
DateTimeFormatter inFormatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(inValue, inFormatter);
DateTimeFormatter outFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SS", Locale.ENGLISH);
String outValue = outFormatter.format(ldt);
System.out.println(outValue);
Will print 2018-05-10 15:48:23.00
Thank you for your response. I actually want the end result as Calendar Object with required format, not a string.
Calendar is effectively deprecated, you shouldn't be using it anymore. Even if you're not using Java 8+, you should be using the ThreeTen Backport API
Calendar (and Date and all other "date/time" class) are just containers of a value representing some point in time, they do not have any kind of "formatting" capabilities of their own. This is why the API has formatting classes. Keep you date/time values represented as appropriate classes until you need to display them, at that point, you should format the value to a String
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 5 years ago.
I've a problem with date. I've a date as string in format: "2017-05-10 16:30"
I'd like to convert it to date looking the same as I wrote before.
Please help me, thanks in advance!
You can use LocalDateTime.parse to create a LocalDateTime object, but the second part of your question didn't really make sense. The date object itself doesn't have a format, so it can't "look like" anything. You decide what format to adopt when you convert it back to a string.
A simple search in google or stackoverflow might lead to answer but here you go.
Pre Java 8
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2017-05-10 16:30");
Java 8
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime ldt = LocalDateTime.parse("2017-05-10 16:30", dtf);