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);
Related
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);
This question already has answers here:
How do I convert the date from one format to another date object in another format without using any deprecated classes?
(10 answers)
Closed 4 years ago.
How do I convert date format from
dd-mm-yyyy
to
YYMMDD
in java?
I think you are seeking the solution like date formatting. You can try to use DateTimeFormatter with your own pattern.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy-MM-dd");
This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 7 years ago.
I have this weird date format that I have to parse.
2015-12-18T03:36:06.000+0000
I am currently mapping a regex to date formats so I can parse different dates. However, this format got me confused. Any help appreciated.
To parse a String into a Date in Java, you use a DateFormat object, and specify the format the date is in. There is no need to use a Regex, the Java library has a way to do this for you.
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
final Date d = df.parse("2015-12-18T03:36:06.000+0000"); // From your code above
System.out.println(d);
See the JavaDoc for SimpleDateFormat for more explanation as to what the symbols mean. This is actually a common format for dates called ISO 8601, I just took the pattern right from the documentation.
Watch out! These DateFormat objects are not threadsafe.
This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 8 years ago.
This is what I tried so far but is not working:
This is the format of the date return by the Drive api's json response
2014-04-29T17:58:02.437Z
final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
System.out.println(df.parse("2014-04-29T17:58:02.437Z"));
What is the correct way to convert it?
Quick suggestion for:
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.parse("2014-04-29T17:58:02.437Z"));
As you see, Z is simply set by GMT, while you was not parsing 437 milliseconds.
Works for me, just tried it out!
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert a date String to a Date or Calendar object?
I'm in a pinch here...
I have a series of date/time strings formatted like this:"9-29-2011 9:05 PM PDT"
and I need to convert this string into a java Calendar object.
Once I have this Calendar object it must represent exactly this
date and time (including the AM or PM). Please, what is the best way to accomplish this?
Use DateFormat to parse the String. Pass the Date to the Calendar.setTime() method.
Check out http://joda-time.sourceforge.net/ . Joda Time makes a lot of tricky date/time operations simple!
You will probably want to construct a custom formatter: http://joda-time.sourceforge.net/userguide.html#Input_and_Output
DateTimeFormat.forPattern("dd-MM-yyyy hh:mm a z"); should be close. You can then convert the Joda DateTime to Date or to a Calendar.