Best way to transform date string into an date object [duplicate] - java

This question already has answers here:
How to parse date string to Date? [duplicate]
(6 answers)
Java string to date conversion
(17 answers)
Closed 6 years ago.
What is the best way to transform like October-2016 into an Date Object?
When I try to use Date date = new SimpleDateFormat("MMMM-yyyy").parse("October-2016");
it throws an exception, because it cannot convert this type of date.

since the string October-2016 is containing information related to a language YOU NEED TO SPECIFY the Locale in the instance of the SimpleDateFormat
Date date = new SimpleDateFormat("MMMM-yyyy", Locale.US).parse("October-2016");

String st = "October-2016";
DateFormat format = new SimpleDateFormat("MMMM-yyyy");
Date date = format.parse(st);
System.out.println(date);
Both working same:
Date date = new SimpleDateFormat("MMMM-yyyy").parse("October-2016");
Date date1 = new SimpleDateFormat("MMMM-yyyy", Locale.US).parse("October-2016");
System.out.println(date);
System.out.println(date1);

Related

How to convert "2019-04-24" to "24-Apr-2019" date format? [duplicate]

This question already has answers here:
java.util.Date is generating a wrong date?
(3 answers)
Java SimpleDateFormat always returning January for Month
(4 answers)
Java Date() giving the wrong date [duplicate]
(9 answers)
Closed 1 year ago.
I am trying to convert "2019-04-24" to "24-Apr-2019" this but it is converting it as "24-Jan-19".
This is the code I am using:
public static String dateFormatConvert(String date, String srcDateFormat, String targetDataFormat) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(srcDateFormat);
Date srcDate = dateFormat.parse(date);
DateFormat destDateFormat = new SimpleDateFormat(targetDataFormat);
return destDateFormat.format(srcDate);
}
Calling it:
String date1 = CommonUtil.dateFormatConvert("2019-04-24", "yyyy-MM-DD", "DD-MMM-YY"); -> This is one of the format used (DD-MMM-YY) out of many
System.out.println(date1);
What's going wrong here?
According to the Documentation.
D - Day in year
d - Day in month
The correct code would be:
date1 = dateFormatConvert("2019-04-24", "yyyy-MM-dd", "dd-MMM-yyyy");

Convert string yyyy-mm-dd to date in javaEE [duplicate]

This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
(8 answers)
Calculating days between two dates with Java
(16 answers)
Converting String to Date using SimpleDateFormat is returning random date [duplicate]
(2 answers)
Closed 2 years ago.
I need to convert the following format of date yyyy-mm-dd to date in format dd/mm/yyyy or dd-mm-yyyy, is it posible. I'm getting unparsable exception.
I have the following code:
String fechaInicial= docudetalle.getfechainicial();
String fechaFinal= docudetalle.getfechafinal();
String fechaEspecial= docudetalle.getfechaespecial();
SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
Date dateInicial = formatter.parse(fechaInicial);
Date dateFinal = formatter.parse(fechaFinal);
Date dateEspecial = formatter.parse(fechaEspecial);
To which once converted I need to get the difference between the three dates, to show in a table as the amount days passed. How can I get the difference in the dates, I know with calendar instance is much easier to get the difference. Is there is an easier method for this?
Use LocalDate and DateTimeFormatter to manipulate dates.
String source = "2020-04-22";
LocalDate src = LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String dst = src.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
System.out.println(dst);
// or
dst = src.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
System.out.println(dst);
prints
22/04/2020
22-04-2020

JAVA - Format a custom string using SimpleDateFormat [duplicate]

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Java string to date conversion
(17 answers)
Closed 5 years ago.
I'm having trouble formatting a custom String back to a Date object. What i have:
String customString = "October 14, 2015;
Date date = new Date();
SimpleDateFormat s = new SimpleDateFormat("MM-dd-yyyy");
try {
date = s.parse(customString);
} catch (ParseException e) {
e.printStackTrace();
}
I always get a unappeasable date exception. Any pointers of what i'm doing wrong is appreciated.
Your pattern must be: new SimpleDateFormat("MMM dd,yyyy");
For more informations about SimpleDateFormat see the javadoc
Read the docs, https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html.
The argument of the constructor should have the format of the date you want to input.
For instance, if you want the full month name should have "MMMMM".
Just make the following change and the program will work.
SimpleDateFormat s = new SimpleDateFormat("MMMMM dd, yyyy");

Convert String TimeStamp"2016-12-26 19:26:36.915" To Standard Timestamp format "yyyy-MM-dd hh:mm:ss.sss" [duplicate]

This question already has answers here:
Java: Convert String to TimeStamp
(11 answers)
Closed 6 years ago.
I need to convert Convert String TimeStamp"2016-12-26 19:26:36.915" To Standard Timestamp format "yyyy-MM-dd hh:mm:ss.sss" using core java.
Please help me out the standard way to code this.
try{
String inputTime = "2016-12-26 19:26:36.915";
String date_format = "yyyy-MM-dd hh:mm:ss.sss";
DateFormat dateFormat = new SimpleDateFormat(date_format);
//You will get a date object below from String you provide
Date date = dateFormat.parse(inputTime);
}catch(ParseException exception){
System.out.println(exception.toString());
}

java - Convert date string to UTC time [duplicate]

This question already has answers here:
Parsing ISO-8601 DateTime with offset with colon in Java
(4 answers)
Closed 6 years ago.
I just needed a sample code block or suggestion to convert the following date string to utc time and find difference with current time in java?
date string "2016-03-21T15:58:36-04:00"
Thanks in advance
You need to format the date string in following way:
String string = "2016-03-21T15:58:36-04:00";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
Date date = format.parse(string);
Then you just need to use Date APIs to find the time difference.
public static long getMillisFrom(String inStrDate) {
DateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
Date inDate = ISO_DATE_FORMAT.parse(inStrDate);
Date curDate = new Date();
long diffMillis = curDate().getTime() - inDate.getTime();
return diffMillis
}

Categories