Convert string with millisecond and timezone to Date - java

I have string in this format:
2017-04-06T09:29:12.225Z
I need it in date format i tried with:
private Date convertStringToDate(String dateString) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sssZ");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertedDate;
}
But it showing Unparsable error?

Since Java 7 you can use
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
The X stands for the ISO 8601 time zone, which may be simply Z (the zero time zone) or something like -08, -0800 or -08:00.
See also the table given in the javadoc of SimpleDateFormat.

Try milliseconds should be capital S:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

Try
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

The milliseconds should be capital S, and the Z should be in single quotes:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

Related

How to get date from date time in java

This is my date string,
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
I want to get only date with "yyyy-MM-dd" format.
How can I get date with this format.
I tried with below code, But it's not working.
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
Date date = null;
try {
date = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss Z").parse(dd);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println("\n"+newstr+"\n");
java.time
Using java.time, the modern Java date and time API, it’s pretty simple when you know how:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"MMM-dd-uuuu HH:mm:ss.SSS zzz(xx)", Locale.ROOT);
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
LocalDate date = LocalDate.parse(dd, formatter);
System.out.println(date);
Output is:
2019-11-08
As we can see, your string is in UTC. I have assumed that you also want your date in UTC. If not, we need to parse into a ZonedDateTime, convert to your desired zone and then format into your desired output format with only the date.
I assumed that UTC(+0000) is a time zone abbreviation followed by an offset in brackets.
The date and time classes that you were trying to use, Date and SimpleDateFormat, are poorly designed and long outdated, the latter in particular notoriously troublesome. You should not use them. java.time is so much nocer to work with.
What went wrong in your code?
MM is for two-digit month number, for example 11 for November or 08 for August. For month abbreviation like Nov you need MMM.
Z is for offset like +0000. While this does appear in your string, the text UTC comes before it. Edit: Confusingly SimpleDateFormat parses UTC as time zone and then ignores the remainder of the string. I for my part shouldn’t want to rely on this behaviour.
I recommend you specify a locale with your formatter to control which language it expects in the string. With MMM for month abbreviation, if you rely on the default locale and it happens to be Welsh, your formatter will expect Tach for November.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Related question: Why can't this SimpleDateFormat parse this date string?
Don't use SimpleDateFormat and Date classes those are legacy, Use java-8 modern date time API classes, First create DateTimeFormatter with the input format date
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM-dd-uuuu HH:mm:ss.SSS zzz(Z)");
And then use OffsetDateTime to parse it with particular offset (In your case UTC)
OffsetDateTime dateTime = OffsetDateTime.parse(dd,formatter);
And then get the LocalDate
System.out.println(dateTime.toLocalDate());
The pattern you provide in the SimpleDateFormatter class constructor should match with the provided output.
e.g. since you are providing month as three letters, use 3 M's in the pattern.
String dd = "Nov-08-2019 07:00:28";
Date date = null;
try {
date = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss").parse(dd);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println("\n"+newstr+"\n");
}
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
Date date = null;
try {
date = new SimpleDateFormat("MMM-dd-yyyy hh:mm:ss.S z").parse(dd);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println("\n"+newstr+"\n");
This might help you.
Your input string is
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
You want is
yyyy-MM-dd
first it is not possible you should be getting yyyy-MMM-dd for that the code will be
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
Date date = null;
try {
date = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss Z").parse(dd);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println("\n"+newstr+"\n");
"Nov" is known as "MMM" format so it will throw the exception.
Try this code: https://onlinegdb.com/H1keFI5oS
import java.util.Date;
import java.text.SimpleDateFormat;
public class Main
{
public static void main(String []args){
String dd = "Nov-08-2019 07:00:28.190 UTC(+0000)";
Date date = null;
try {
date = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss").parse(dd);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String newstr = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println("\n"+newstr+"\n");
}
}

java.text.ParseException: Unparseable date: "2018-05-23T06:39:37+0000"

I'm trying to create a Date from a String I receive from the server. The String is:
2018-05-23T06:39:37+0000
So the correct format should be:
yyyy-MM-dd'T'HH:mm:ss.SSSZ
Here is my code:
String createdDate = comment.getCreatedDateTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
try {
Date parsedDate = simpleDateFormat.parse(createdDate);
createdDate = parsedDate.toString();
} catch (ParseException ex) {
ex.printStackTrace();
}
mCommentDate.setText(createdDate);
I don't know if there is any way to do this, because after that I would like to parse again to the next format:
dd/MM/yyyy hh:mm
I've tried to parse the original String using this last format directly but I'm getting the same exception.
Any suggestion?
I see you've solved your own problem with a little help from the comments, however I would suggest you seriously consider LocalDate, as the older Date classes are quite troublesome at times.
In fact, as your incoming value has a TimeZone, you'll need to use ZonedDateTime to parse your input.
String createdDate = "2018-05-23T06:39:37+0000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
ZonedDateTime localDate = ZonedDateTime.parse(createdDate, formatter);
System.out.println(localDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")));
Output:
23/05/2018 06:39
The given input date String format
2018-05-23T06:39:37+0000
is incorrect so that you are getting ParseException since millisecond(SSS) part is missing from your date format yyyy-MM-dd'T'HH:mm:ss.SSSZ
So please try with
2018-05-23T06:39:37.235-0530
so below code should work
String createdDate = comment.getCreatedDateTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
try {
Date parsedDate = simpleDateFormat.parse(createdDate);
createdDate = parsedDate.toString();
System.out.println(parsedDate.toString());
} catch (ParseException ex) {
ex.printStackTrace();
}
mCommentDate.setText(createdDate);
Ok, the first mistake (as you've pointed) is I didn't have milliseconds on the original String.
After removing "SSS" from the simpleDateFormat it works like a charm. So this is the final code:
String createdDate = comment.getCreatedDateTime();
SimpleDateFormat defaultDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.getDefault());
try {
Date parsedDate = defaultDateFormat.parse(createdDate);
SimpleDateFormat finalDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.getDefault());
createdDate = finalDateFormat.format(parsedDate);
} catch (ParseException ex) {
ex.printStackTrace();
}
mCommentDate.setText(createdDate);

How to save date (dd.mm.yyyy) in java

I am getting a date in the form of dd.mm.yyyy and want to save it as a proper object. It should be comparable to another date object. How do I realize this?
Use SimpleDateFormat as this:
Date date = new SimpleDateFormat("dd/MM/yyyy").parse("12/05/2015");
Using JodaTime
String input = "03.01.2015";
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd.MM.yyyy");
DateTime dt = DateTime.parse(input, formatter);
DateTime now = new DateTime();
System.out.println(dt.compareTo(now));
Use SimleDateFormat
String string = "03.01.2015";
DateFormat format = new SimpleDateFormat("MM.dd.yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
Try this code
try {
SimpleDateFormat sdf= new SimpleDateFormat("dd.MM.yyyy");
Date d = sdf.parse("19.05.2090");
System.out.println(d);
} catch (ParseException ex) {
ex.printStackTrace();
}

date conversion issue in java

I accept the user input date in yyyy/mm/dd format. When I try to convert using SimpleDateFormat it shows "null";
my code for conversion is:
SimpleDateFormat dateofbirthFormat = new SimpleDateFormat("yyyy/mm/dd");
java.util.Date dateOfBirth = dateofbirthFormat.parse(birthdate);
why is it showing null?
MM not mm,
MM is for months,
mm is for minutes
SimpleDateFormat dateofbirthFormat = new SimpleDateFormat("yyyy/MM/dd");
try {
java.util.Date dateOfBirth = dateofbirthFormat.parse("1986/12/11");
System.out.println(dateOfBirth);
} catch (ParseException e) {
}
By yyyy/mm/dd I guest you mean years/Months/days. So you may try using
yyyy/MM/dd (note upcase use of MM).
SimpleDateFormat dateofbirthFormat = new SimpleDateFormat("yyyy/MM/dd");
java.util.Date dateOfBirth = dateofbirthFormat.parse(birthdate); .

How to convert the following string to date or calendar object in Java?

Have a string like 2011-03-09T03:02:10.823Z, how to convert it to Date or calendar object in Java?
You can use SimpleDateFormat#parse() to convert a String in a date format pattern to a Date.
String string = "2011-03-09T03:02:10.823Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
Date date = new SimpleDateFormat(pattern).parse(string);
System.out.println(date); // Wed Mar 09 03:02:10 BOT 2011
For an overview of all pattern characters, read the introductory text of SimpleDateFormat javadoc.
To convert it further to Calendar, just use Calendar#setTime().
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// ...
I want to show "2017-01-11" to "Jan 11" and this is my solution.
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat df_output = new SimpleDateFormat("MMM DD");
Calendar cal=Calendar.getInstance();
Date date = null;
try {
date = df.parse(selectedDate);
String outputDate = df.format(date);
date = df_output.parse(outputDate);
cal.setTime(date);
} catch (ParseException e) {
e.printStackTrace();
}
import java.util.*;
import java.text.*;
public class StringToCalender {
public static void main(String[] args) {
try { String str_date="11-June-07";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yy");
date = (Date)formatter.parse(str_date);
Calendar cal=Calendar.getInstance();
cal.setTime(date);
System.out.println("Today is " +date );
} catch (ParseException e)
{System.out.println("Exception :"+e); }
}
}
Your given date is taken as a string that is converted into a date type by using the parse() method. The parse() method invokes an object of DateFormat. The setTime(Date date) sets the formatted date into Calendar. This method invokes to Calendar object with the Date object.refer
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf.parse(strDate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
try this.(strDate id your string format of Date)

Categories