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

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

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");

Formatting LocalDate in Java [duplicate]

This question already has answers here:
Get first and last day of month using threeten, LocalDate
(13 answers)
How to format LocalDate to yyyyMMDD (without JodaTime)
(2 answers)
Closed 3 years ago.
I defined the following format in Java :
//define format of YYYYMMDD
private final DateTimeFormatter dtf = DateTimeFormatter.BASIC_ISO_DATE;
My application fetches a certain date from the calendar:
LocalDate localDate = fetchDate(); // fetch date in format "yyyy-mm-dd"
I want to store an additional two dates in format of dtf. The startOfMonth and endOfMonth of the given localDate.
E.g. if localDate is "2019-12-12" I want to create the following two variables -
String startOfMonth = "20191201";
String endOfMonth = "20191231";
How can I do that?
LocalDate atStartOfMonth = localDate.with(TemporalAdjusters.firstDayOfMonth());
LocalDate atEndOfMonth = localDate.with(TemporalAdjusters.lastDayOfMonth());
And then you format those local dates the way you want to (i.e. with your dtf formatter)

dd/MM/yyyy Date Format to Monthname day,Year Java Android [duplicate]

This question already has answers here:
Parse String to Date with Different Format in Java
(10 answers)
Closed 4 years ago.
First i have a java Date object lets say
Date firstdate; // its initialized and equal to some date dont worry
with using this code i convert it to dd/MM/yyyy format
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
date.setText(sdf.format(firstdate));
But since sdf.format is returning to a string,after formating 1 time i cant format it again to my desired Monthname day,Year format before displaying to user.
i mean if sdf.format(firstdate) returns "01/01/2000" string,i would like to convert this to January 1,2000 and then display it to user.
First get the String and convert to a Date:
String s = date.getText(); // e.g. "01/01/2000"
Date d = sdf.parse(s); // parse the String to get a Date object
Then you need a second SimpleDateFormat to convert the Date to the desired String:
SimpleDateFormat sdf2 = new SimpleDateFormat("MMMMM dd, yyyy");
s = sdf2.format(d); // "January 1, 2000"

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

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);

Convert date with format "yyyy-mm-ddTHH:MM:SS+/-0000" to "yyyy-mm-ddTHH:MM:SSZ" [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I need to convert date string into a another specific format.
For example: I've a date which can come as YYYY-MM-DD or YYYY-MM-DDThh:mm:ss±0000 and I need to convert that to something "yyyy-mm-ddTHH:MM:SSZ".
I tried few things using Java 8 API but could not find a way out.
Try this (Java 8):
String input = "2016-10-14T14:19:40-04:00";
String output = ZonedDateTime.parse(input).toInstant().toString();
System.out.println(output); // prints 2016-10-14T18:19:40Z
This works because the primary format of ZonedDateTime used by parse() is ISO_ZONED_DATE_TIME, so it will parse input in the format you described.
The output format you requested happens to be the default format of Instant, as returned by its toString() method, i.e. the ISO_INSTANT format.
Converting between the two (ZonedDateTime to Instant using the toInstant() method) will perform the timezone adjustment necessary.
Try this:
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd"); // or "YYYY-MM-DDThh:mm:ss±0000"
String dateInString = "1993-11-27";
Date date = inputFormat.parse(dateInString);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ssZ");
System.out.println(outputFormat.format(date));

Categories