I am trying to import a given string representing a Date in the format:
2007-03-12T00:00:00.000+01:00
Now to create a new Date Object i use Joda Library using:
DateTime date = new DateTime(year, month, day, hour, minute, second);
However, i want to make sure two things here:
How to handle GTM +1 in this date time context?
Is there anyway,
that i don't have to parse this string, and the Date Object can be
initialized directly with this string?
DateTime date = DateTime.parse("2007-03-12T00:00:00.000+01:00");
As has been mentioned in other answers, the offset is supposed to be parsed along with the rest of the string according to the documentation.
You can parse that date string using SimpleDateFormat, then pass that Date into a Joda class:
String dateStr = "2007-03-12T00:00:00.000+01:00";
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = sdf.parse( dateStr.replaceAll(":(?=..$)", "")); // remove last colon
Note that you must remove the last colon so the offset is a RFC 822 time zone like +0100, which I did using String.replaceAll()
Both your questions can be answered by reading the documentation for the class
timezone is handled by the class. Look for the constructor which takes the timezone argument.
Yes you can create the DateTime object using the string. DateTime.parse(String) is available to do that. There is also another method available to parse custom date formats if required.
Related
This question already has answers here:
Converting a date string to a DateTime object using Joda Time library
(10 answers)
Closed 6 years ago.
Is there a way to convert a date in the format "YYYY-MM-dd" to "YYYY-MM-dd HH:mm:ss" using Joda?
Eg: "2016-01-21" to "2016-01-21 00:00:00"
Use DateTimeFormat class from Joda API. It helps you to format the date to the formatting of your choice. You can simply provide the format you want, like in this case you want "YYYY-MM-dd HH:mm:ss". The code below works with JodaTime 2.0 and above.
DateTime date = DateTime.parse("2016-01-21", DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss"));
There are two things in play here, first we need to parse the existing string into a DateTime object, which is done via the parse method, it also allows an additional argument, to convert the output into a different format. The longer but easier to understand implementation is given below.
DateTime date = DateTime.parse("2016-01-21");
DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss");
date = formatter.parseDateTime(string);
Your question is not clear:
Do you want to just format "something representing a date" into a string with time of "00:00:00"?
Or are you trying to convert "something representing a date" into "something representing a date+time, with 00:00:00 as time"?
Or are you trying to convert a java.util.Date to a Joda org.joda.time.DateTime by ignoring the original time and set time to 00:00:00?
Or are you trying to convert a string of date with format of "YYYY-MM-dd" to another String with date+time, with 00:00:00 as time?
Or something else?
In Joda, the proper way to represent a date is by LocalDate, and the proper way to represent a "date + time" information (but not a instant of time) is by LocalDateTime. DateTime is representing a instant of time. With these basic understanding:
Answer for Q1:
String result = DateTimeFormat.forPattern("YYYY-MM-dd", myLocalDate);
Answer for Q2:
LocalDateTime result = myLocalDate.toLocalDateTime(LocalTime.MIDNIGHT);
Answer for Q3:
DateTime result = new DateTime(javaUtilDate).withTimeAtStartOfDay();
Answer for Q4:
String result = dateString + " 00:00:00";
I want to insert into a Mysql column (DATETIME column) a time & date . For that purpose i use
the DATETIME field .
But on the JAVA side , I need to have a Date object with the time & date .
Consider the code :
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String str = dateFormat.format(cal.getTime());
System.out.println(str);
This code produces :
2014/03/19 01:11:35
How can I convert it to a Date object in the format yyyy/MM/dd HH:mm:ss , so it would be
possible to put it in the mysql column of DATETIME ?
Much appreciated
The getTime() method of Calendar returns a Date object.
For JDBC, you'll use a java.sql.Timestamp to preserve the time component. (I think a java.sql.Date loses the time component.)
For example:
preparedStatement.setTimestamp(new java.sql.Timestamp(cal.getTime()));
First point: Date objects have no "format" - they just represent an instant in time.
If you print them, you get the default toString() implementation for Date.
Second point: You don't need to format a Date to use it with JDBC.
You should use a prepared statement, then call setObject(), to set the parameter value - the JDBC driver will do the rest, including wrapping in quotes if that's required for the data type.
Final point: Never use Calendar unless you have to.
In this case, simply new Date() will do the job.
How can I get the current date of the system with this format yyyy-MM-dd
I want this
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String strDate = formatDate.format(now);
return strDate;
but returning a Date instead of a String.
Any help is greatly appreciated.
UPDATE: If that's the case, can I parse my String to Date?
How can i get the current date of the system with this format yyyy-MM-dd but returns Date instead of String.
You can't. There's no such thing as "a Date with a format" any more than there's the concept of "an int with a format". A Date value is just a point in time, with no associated text format, calendar system or time zone.
Using new Date() will get you a Date object representing the current instant in time, and nothing else. How you use that is up to you - but if you return it from a method then there is no associated date (as the date will vary by time zone), no format etc - it's up to the calling code to use it appropriately.
You might want to consider using Joda Time which at least has a LocalDate type - although you still need to consider which time zone you want to use when you think about "the current date". (And there's still no formatting information associated with the value.)
EDIT: To answer your update, you can just use SimpleDateFormat to parse - but it's not clear where your string has come from to start with. This sounds like the opposite requirement from the rest of your question.
since you cant change Date format build your own CustomDate, it is just a representation of time.
on the method which recieves the date as a string
use another simpledateformatter
and convert the string into date by using
simpledateformatter.parse(strDate);
You can use this .!!
String formatDate = new SimpleDateFormat("yyyy-MM-dd").format( yourDate);
I am reading date values from a file in which i am reading it as string object.
The date values varies in many format for example sew below:
01-Mar-2012
01/12/2012
01-12-2012
01.12.2012
01.Mar.2012
07/01/2008 12:00:00
07/01/2008 12:00:00 AM
What ever be the format i want the date object in format dd/mm/yyyy.
There is so many combinations.
SimpleDateFormat expect me to provide the pattern to format it .
Is there is any way to guess the pattern or create a date object from the given string?
how can you know if 12-6-2012 is 6th december or 12th june?!
what you could do is defining an array with all possible patterns.
then try to parse the date for each array entry (if it throws an exceptionen try the nexct pattern and so on)
i know that this is a pretty ugly attempt but it works!
What ever be the format i want the date object in format dd/mm/yyyy.
A java.util.Date object does not have a format - the only information it contains is an instant in time (specifically, the number of milliseconds since 01-01-1970, 00:00:00 GMT).
So, you cannot have a "date object in format dd/mm/yyyy". Date objects don't have a format, just like numbers don't have an inherent format.
If you want to display the date in the format dd/mm/yyyy, then you have to convert it to a string first using a SimpleDateFormat object; you specify the format on the SimpleDateFormat object (not on the Date object itself).
// NOTE: use MM instead of mm; MM = months, mm = minutes
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date now = new Date();
// Display the date in the format dd/MM/yyyy
System.out.println(df.format(now));
I am getting a date/time string from web in the format of "yyyy/mm/dd'T'HH:MM:SS'Z'" and it is in UTC.
Now I have to identify the current time zone of device and then convert this time to my local time..
How do I do it?
(FYI, Currently, UTC time is 10:25 AM, in India current time is 3:55 PM)
Try using TimeZone.getDefault() instead of TimeZone.getTimeZone("GMT")
From the docs:
... you get a TimeZone using
getDefault which creates a TimeZone
based on the time zone where the
program is running.
EDIT: You can parse date using SimpleDateFormat (there is also the documentation on the format string there). In your case, you want to do (untested):
// note that I modified the format string slightly
SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd'T'HH:mm:ss'Z'");
// set the timezone to the original date string's timezone
fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = fmt.parse("1998/12/21T13:29:31Z", new ParsePosition(0));
// then reset to the target date string's (local) timezone
fmt.setTimeZone(TimeZone.getDefault());
String localTime = fmt.format(date);
alternatively, use two separate instances of SimpleDateFormat, one for original and one for target time.