I have the following date:
2011-10-07T08:51:52.006Z
Now I want to parse it into a GregorianCalendar. Is there an easier way to do it than using substrings and parsing them to Integers?
And what is the Z in the time string?
I tried to parse it using SimpleDateFormat, but I canĀ“t find a explanation for the T in the date String.
DateFormat format = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" )
Date date = format.parse( "2011-10-07T08:51:52.006Z" );
Calendar calendar = new GregorianCalendar();
calendar.setTime( date );
I would take a look at DateTimeFormatter
DateTimeFormatter formatter =
DateTimeFormat.forPattern("<custom_pattern>").withOffsetParsed();
DateTime dateTime = formatter.parseDateTime("<your_input>");
GregorianCalendar cal = dateTime.toGregorianCalendar();
The T in your string acts as a separator between the date and the time and the Z is the time-zone information both as per ISO-8601 format.
You could use the SimpleDateFormatter to parse the String. Please read the javadoc for the aforementioned class to know what could be the format string. 'Z' indicates the timezone information.
Related
I'm using Joda time DateTimeFormatter to create a new datetime object in yyyy-mm-dd string format. When calling DateTime date = formatter.parse(String) I get a DateTime object with an extra hour. I live in UTC +1. How to format a string datetime without hours added.
String date = "2013-01-28";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-mm-dd");
DateTime date = formatter.parseDateTime(date);
date.ToString() = 2013-01-28T00:01:00.000+01:00
expected = 2013-01-28T00:00:00.000+01:00
Additionally, later in the code I compare two DateTime objects. This parser is in yymmdd format and it does not add one hour.
String date = "130102"
DateTimeFormatter format = DateTimeFormat.forPattern("yyMMdd");
DateTime datetime = format.parseDateTime(date);
datetime.toString = 2013-01-02T00:00:00.000+01:00
yyyy-mm-dd uses the minute-of-hour, not the month (pattern symbol M). Please refer to the documentation of pattern symbols on Joda-Time-page.
Parsing "2013-01-28" with your wrong pattern yields "2013-01-28T00:01:00.000+01:00". Do you see the minute equal to 1? And the month of January seems to be a default value if the parser cannot find a month information (in my opinion not smart).
I want to parse this json string into date
"startDateTime":"2014-08-10T20:08:45.0218Z"
and then parse it to another date format.
I thought using this:
Gson gson = new GsonBuilder().setDateFormat("dd/MM HH:mm ???").create;
but I'm not sure how what is the format of "2014-08-10T20:08:45.0218Z"
is it yyyy-mm-dd ???
2014-08-10T20:08:45.0218Z
This looks like an ISO 8601 date
Date and time expressed according to ISO 8601:
Combined date and time in UTC: ... 2014-08-20T19:23:25Z
so
yyyy-MM-dd'T'HH:mm:ss.SSSZ
should do it.
Joda time also provides ISODateTimeFormat since this is a common format.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String date = sdf.format(new Date());
System.out.println(date);
Date d = sdf.parse(date);
I'm running the program written below, but instead of printing in mm/dd/yyyy hh:mm format it prints in the normal date format(ie. Day Date and time)
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy hh:mm");
Date date = sdf.parse(sdf.format(Calendar.getInstance().getTime()));
The reason i'm doing this is because the existing method accepts parameters in Date format, so i need to send the above mentioned date object to it.
Please point out the mistake or suggest some other alternative.
Thanks
Date objects don't have a format. The Date class is a wrapper around a single long, the number of milliseconds since the epoch. You can't "format" a Date, only a String. Pass around a Date/Calendar internally, and format it whenever you need to display it, log it, or otherwise return it to the user.
Change the format to MM/dd/yyyy. Month is denoted by capital M.
Check below URL for valid formats
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Your formatter works quite fine (apart from the mm vs. MM bug). You get a formatted string from the date and then create a copy from your date by parsing the formatted string:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm");
Date now = Calendar.getInstance().getTime();
String formattedNow = sdf.format(now); // == "09/24/2013 01:59"
Date now2 = sdf.parse(formattedNow); // == now
how to format "2011-10-25T13:00:00Z" string into date and time
i used simple date format class
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
but it only giving the date value. not time values
please help me to solve this problem
Use the format "yyyy-MM-dd'T'HH:mm:ss'Z'" for parsing this date format. See the documentation of SimpleDateFormat for more info. Code will look like this
String dateStr = "2011-09-19T15:57:11Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
Date date = new SimpleDateFormat(pattern).parse(dateStr);
This is because "yyyy-MM-dd" only mentions year (yyyy), month (MM) and date (dd). Try adding hh:mm if you want hours and minutes.
Example:
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd hh:mm");
System.out.println(sim.format(new Date())); // prints "2011-10-27 01:56"
The full documentation of the format-string and its parts is found here. The documentation includes this example:
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" - 2001-07-04T12:08:56.235-0700
Perhaps it's something like that you're looking for.
I want to convert the timestamp 2011-03-10T11:54:30.207Z to 10/03/2011 11:54:30.207. How can I do this? I want to convert ISO8601 format to UTC and then that UTC should be location aware. Please help
String str_date="2011-03-10T11:54:30.207Z";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
date = (Date)formatter.parse(str_date);
System.out.println("output: " +date );
Exception :java.text.ParseException: Unparseable date: "2011-03-10T11:54:30.207Z"
Firstly, you need to be aware that UTC isn't a format, it's a time zone, effectively. So "converting from ISO8601 to UTC" doesn't really make sense as a concept.
However, here's a sample program using Joda Time which parses the text into a DateTime and then formats it. I've guessed at a format you may want to use - you haven't really provided enough information about what you're trying to do to say more than that. You may also want to consider time zones... do you want to display the local time at the specified instant? If so, you'll need to work out the user's time zone and convert appropriately.
import org.joda.time.*;
import org.joda.time.format.*;
public class Test {
public static void main(String[] args) {
String text = "2011-03-10T11:54:30.207Z";
DateTimeFormatter parser = ISODateTimeFormat.dateTime();
DateTime dt = parser.parseDateTime(text);
DateTimeFormatter formatter = DateTimeFormat.mediumDateTime();
System.out.println(formatter.print(dt));
}
}
Yes. you can use SimpleDateFormat like this.
SimpleDateFormat formatter, FORMATTER;
formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String oldDate = "2011-03-10T11:54:30.207Z";
Date date = formatter.parse(oldDate.substring(0, 24));
FORMATTER = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS");
System.out.println("OldDate-->"+oldDate);
System.out.println("NewDate-->"+FORMATTER.format(date));
Output
OldDate-->2011-03-10T11:54:30.207Z
NewDate-->10-Mar-2011 11:54:30.207
Enter the original date into a Date object and then print out the result with a DateFormat. You may have to split up the string into smaller pieces to create the initial Date object, if the automatic parse method does not accept your format.
Pseudocode:
Date inputDate = convertYourInputIntoADateInWhateverWayYouPrefer(inputString);
DateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss.SSS");
String outputString = outputFormat.format(inputDate);
You might want to have a look at joda time, which is a little easier to use than the java native date tools, and provides many common date patterns pre-built.
In response to comments, more detail:
To do this using Joda time, you need two DateTimeFormatters - one for your input format to parse your input and one for your output format to print your output. Your input format is an ISO standard format, so Joda time's ISODateTimeFormat class has a static method with a parser for it already: dateHourMinuteSecondMillis. Your output format isn't one they have a pre-built formatter for, so you'll have to make one yourself using DateTimeFormat. I think DateTimeFormat.forPattern("mm/dd/yyyy kk:mm:ss.SSS"); should do the trick. Once you have your two formatters, call the parseDateTime() method on the input format and the print method on the output format to get your result, as a string.
Putting it together should look something like this (warning, untested):
DateTimeFormatter input = ISODateTimeFormat.dateHourMinuteSecondMillis();
DateTimeFormatter output = DateTimeFormat.forPattern("mm/dd/yyyy kk:mm:ss.SSS");
String outputFormat = output.print( input.parseDate(inputFormat) );
Hope this Helps:
public String getSystemTimeInBelowFormat() {
String timestamp = new SimpleDateFormat("yyyy-mm-dd 'T' HH:MM:SS.mmm-HH:SS").format(new Date());
return timestamp;
}
Use DateFormat. (Sorry, but the brevity of the question does not warrant a longer or more detailed answer.)