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.
Related
This question already has answers here:
LocalDate to java.util.Date and vice versa simplest conversion? [duplicate]
(7 answers)
How do I get a Date without time in Java?
(23 answers)
Closed 3 years ago.
I want to get a Date without time, but always failed.
below is my codes:
long curLong = System.currentTimeMillis();
curLong = curLong - curLong % TimeUnit.DAYS.toMillis(1);
Date date = new Date(curLong);
System.out.println("date = " + date);
the output:
date = Mon Oct 28 08:00:00 CST 2019
anyone knows why? Thank you
It is not recommended to use java.util.Date anymore. It was called Date but doesn't necessarily hold only the date information but information about the time additionally.
Use this:
LocalDate today = LocalDate.now();
and print it as
System.out.println(today.format(DateTimeFormatter.ISO_DATE);
using the ISO date format. You can define your own formatting pattern using a
DateTimeFormatter.ofPattern("dd.MM.yyyy");
for example.
You can use java.time.LocalDate.now() to get just the date.
Anyway, your case doesn't work as you expect because you are doing nothing to remove the time from the date: you are just "repressing" it, that's why it's zero. If you want to continue this way you could always substring it (substring the Date.toString() of course I meant).
Hope I helped.
java.util.Date's javadoc states:
The class Date represents a specific instant in time, with millisecond precision.
Thats why you have date with time
If you want a date you can use : java.time.LocalDate.now() (Java 8+)
First of all, stop using the old java.util.Date. The new Java 8 date and time API has much better classes for all date and time operations.
The LocalDate class does exactly what you want.
The current date can be obtained by LocalDate.now().
It also has a lot of facilities to add and subtract days, months etc. and it takes into consideration all the calendar special cases for you.
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);
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:
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 8 years ago.
I want to display current date in MM-dd-yy format
Date d1=new Date();
Now I want to change in to the mm-dd-yy pattern
So I formatted it using simple date format
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(MM-dd-yyyy);
Pattern
Now d1 is in string type but I want it as Date type
This is the difference between model and view.
The model here is the Date object (Date date = new Date();) It contains information about what month, day, and year, hour of day, etc. that the date was created with. It doesn't know anything about formatting.
The view is the string generated by the DateFormat object. You can pass the model (the date object) in to the formatter in order to generate multiple views of the model depending on what fields you're interested in, and how you want them presented.
So the date continues to hold the same date fields regardless of what strings the formatter generates for it. The date and the formatted strings are separate objects. Although we say "format a date", the formatting doesn't actually change the date object in any way.
Your format string is incorrect, to get the month you want "MM-dd-yyyy", "mm" means minutes. See the API documentation for SimpleDateFormat to see what the different pattern letters mean.
This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 9 years ago.
I am getting an epoch String from my DB which looks something like this : 1391328000000
I am having a hard time trying to convert it to Java Date.
I tried the following :
private String buildDate(String dateString){
System.out.println("dateString " + dateString);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(Integer.parseInt(dateString));
return formatted;
}
I think you're overthinking about the DateFormat. If I want to simply obtain a Date instance, what I would try is the following:
Date d = new Date(Long.parseLong(dateString));
You need to turn it into a java.util.Date object in order for SimpleDateFormat to handle it. Also, a value like what you quoted needs to be parsed as a long, as it is too large for an int.
That is, change the line where you set formatted to be:
String formatted = format.format(new Date(Long.parseLong(dateString)));
As an aside, if the project you're working on can handle an extra external dependency, switch date/time handling over to the joda library. The stuff in java.util (that is, Date and Calendar) rapidly becomes painful and error-prone to work with.