Java convert Date in string to Date [duplicate] - java

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
how to convert this string "2016-10-08T01:00:00-07:00" to date object in Java?
I want to know what is string format to use with SimpleDateFormat.
I have try
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
Date date = format.parse("2016-10-08T01:00:00-07:00");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Change your format from Z to X will work . Detail is here
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");

Date date = format.parse("2016-10-08T01:00:00-07:00");
should be
Date date = format.parse("2016-10-08T01:00:00-0700");
The time zone should not have a colon delimiting hours and minutes.

You can try SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Best solution to refer # Change date format in a Java string

Related

Java SimpleDateFormat: Pattern - ParseException [duplicate]

This question already has answers here:
Datetime parsing error
(2 answers)
Closed 5 years ago.
I am struggling with a date string, I need to parse into the java ‘Date’ object.
Here is what I have got so far:
try {
String value = "2017‎-‎11‎-‎23T14:00:49.184000000Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'";
SimpleDateFormat parser = new SimpleDateFormat(pattern);
Date date = parser.parse(value);
} catch (ParseException e) {e.printStackTrace();}
It currently throws a ParseException “Unparseable date” and I can’t get it to work.
Any help is highly appreciated!
Thanks
Use Instant from java.time package (java 8) instead, it should look like below
String value = "2017-11-23T14:00:49.184000000Z";
Instant instant = Instant.parse(value);
Date date = Date.from(instant);
System.out.println(date);
you can use timeZone as well like this as another solution.
TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
Calendar cal = Calendar.getInstance(tz);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'");
sdf.setCalendar(cal);
cal.setTime(sdf.parse("2017-11-23T14:58:00.184000000Z"));
Date date = cal.getTime();
System.out.println(date);

Convert String month to Int month [Java] [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 5 years ago.
I'm trying to convert a String month to a int month in Java. I do not want my program to have a lot of logic on it, so I'm trying to do it without having to create a switch case or a Enum. If not possible, i'll have to do deal with it and create that logic...
My String Sample is this:
String date = "2017-Oct-27";
And I want it to be like this:
String date = "2017-10-27";
Can anyone help me please?
Thanks
Please try below code
String input = "2017-Oct-27";
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MMM-dd");
String formattedDate= "";
Date date;
try {
date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
formattedDate = formatter.format(date);
} catch (ParseException e1) {
e1.printStackTrace();
}

Convert a Date dd/MM/yyyy to dd-MM-yyyy in java [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I'm trying to convert a date in dd-MM-yyyy format from YYYY-MM-dd hh:mm:ss.ms
in java using the below code but m unable to get the desired value
String dob = (new SimpleDateFormat("dd-MM-yyyy")).format(customerEntity.getDob().toString());
customerEntity.getDob().toString is providing me this value 1987-06-12 00:00:00.0
But when i'm parsing it to the String dob it produces 163-06-1987 as the output whereas i want the output like 12-06-1987 .
Any help will be appreciable, thanks well in advance
format method in SimpleDateFormat take a Date as argument and not a String
public static void main(String[] args) {
String dateStr = "29/12/2016";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
Date date = sdf.parse(dateStr);
sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(sdf.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
Try parsing your string date into a Date first in format it is coming. Post that pass on that Date object to a format in the format you want your output.
As in below :
Date dob = (new SimpleDateFormat("yyyy-MM-dd")).parse("1987-06-12 00:00:00.0");
String dob1 = (new SimpleDateFormat("dd-MM-yyyy")).format(dob);

Android - How can I convert a date/time string into date and time integers? [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I am trying to convert back a stored date/time string in the format of (MM dd, yyyy, HH:mm [AM/PM]), I followed this post to create the string date/time. My current code properly fetches the stored date time but when I try to parse it and display the Date object "d" I get a null output.
Java code:
public void loadExamData(Cursor cursor) {
exam.setText(cursor.getString(1));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date d = sdf.parse(cursor.getString(2));
} catch (ParseException ex) {
Logger.getLogger(edit_exam_form.class.getName()).log(Level.SEVERE, null, ex);
}
location.setText(cursor.getString(3));
}
First you have to parse the date string with the current pattern and then format it with your desired pattern.
For Example, if your current date string is like this [10/10/2016 14:30].
String curDate = "10/10/2016 14:30";
SimpleDateFormat curDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
SimpleDateFormat desiredDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = curDateFormat.parse(curDate);
String desiredDate = desiredDateFormat.format(date);
// you can use this date string now
} catch (Exception e) {}

How do I set the construtor value from a textfield [duplicate]

This question already has answers here:
Convert a string to a GregorianCalendar
(2 answers)
Closed 6 years ago.
This is the code to get date format
GregorianCalendar calDate = new GregorianCalendar();
The constructor will hold value of format as 2015,7,15
what I want to achieve is to be able to set this format from a text field. For example
GregorianCalendar calDate = new GregorianCalendar(Formats.getText());
But I have errors and is not working.
Please whats is right code?
Resolved with the below code
String nnhh= ""+Firstname.getText();
String someDate = ""+nnhh;
SimpleDateFormat sdf = new SimpleDateFormat("MM,dd,yyyy");
try {
Date date = sdf.parse(someDate);
long dd=date.getTime();
Firstname.setText(""+dd);} catch (ParseException e) {
e.printStackTrace();
}
and dd was in TimeInMillis just as I wanted. Thanks
To be able to parse it, I would suggest you to use something like:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("y M d VV m s H");
The formatter requires all of these elements. (see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html for formatting details).
And this should be all you need:
GregorianCalendar calDate = GregorianCalendar.from(ZonedDateTime.parse(dateToParse, formatter));

Categories

Resources