Unparseable datefrom string [duplicate] - java

This question already has answers here:
Converting String to Date using SimpleDateFormat is returning random date [duplicate]
(2 answers)
how to parse output of new Date().toString()
(4 answers)
Change date format in a Java string
(22 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
How to parse date string to Date? [duplicate]
(6 answers)
Closed 3 years ago.
I have a date time string like this:
Java Code:
String datetimeString = "Tue Apr 10 15:19:06 CEST 2018";
I would like to convert this to a date like this: 2018-04-10
How can I do this?
I have tried this:
Date result;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
result = formatter.parse (datetimeString);
But I get "Unparseable date"

Well since you already applied the brute force method, here's how to use the API.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTest {
public static void main(String[] args) throws Exception {
String datetimeString = "Tue Apr 10 15:19:06 CEST 2018";
String from_format = "E MMM dd HH:mm:ss z yyyy";
String to_format = "yyyy-MM-dd";
DateTimeFormatter from_formatter = DateTimeFormatter.ofPattern(from_format);
DateTimeFormatter to_formatter = DateTimeFormatter.ofPattern(to_format);
LocalDateTime ldt = LocalDateTime.parse(datetimeString, from_formatter);
System.out.println(ldt.format(to_formatter));
}
}
In the DateTimeFormatter class it is important to understand the format symbol AND the
presentation descriptions for proper symbol count.

Related

How I can get date object in format yyyy-MM-dd after date parsing [duplicate]

This question already has answers here:
Java convert date format [duplicate]
(3 answers)
display Java.util.Date in a specific format
(11 answers)
Closed 2 years ago.
I have string date 2020-07-05 22:44:59 in string format, I parse it with SimpleDateFormat with
"yyyy-MM-dd HH:mm:ss a z" date pattern but it gives me date like Sun Jul 05 22:44:59 IST 2020.
So how I can get date in "yyyy-MM-dd HH:mm:ss a z" format in date object.
Input : 2020-07-05 22:44:59
Output : 2020-07-05 22:44:59
I want output int the same format like input.
Thanks for Helping me.
String input = "2020-07-05 22:44:59";
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = parser.parse(input);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String output = formatter.format(date);

Convert 201204121458 to date format 2012/04/12 14:59 in Java [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Change date format in a Java string
(22 answers)
Closed 3 years ago.
I have an issue with java how can convert string 201204121458 to date format 2012/04/12 14:59 using a method ?
Thank you
This code snippet may help:
String str = "201204121458";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
DateTimeFormatter printer = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
String formattedDateTime = dateTime.format(printer); // "2012/04/12 14:59"

How to convert into dd-mmm-yyyy format to dd-mm-yyyy format in swing(core java)? [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
How can I change the date format in Java? [duplicate]
(10 answers)
ParseException when parsing 3 character abbreviated month using SimpleDateFormat
(5 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
Closed 3 years ago.
To convert dd-mmm-yyyy to dd-mm-yyyy format.
String dateofbirth = ((JTextField) dobcalender.getDateEditor().getUiComponent()).getText();//date from jcalender
SimpleDateFormat myFormat = new SimpleDateFormat("dd-MM-yyyy");
tried these codes :
System.out.println(myFormat.format(myFormat.parse(dateofbirth)));
myFormat.format(myFormat.parse(dateofbirth));
showing error parseexception
Based on the format "24 Feb 2019"
SimpleDateFormat from=new SimpleDateFormat("dd MMM yyyy");
SimpleDateFormat to=new SimpleDateFormat("dd-MM-yyyy");
Date frm=from.parse("24 Feb 2019");
System.out.println(frm);//Sun Feb 24 00:00:00 IST 2019
System.out.println(to.format(frm));//24-02-2019
First, you need to parse the String to Date object. Then you need to convert the Date object to a new formatted String. Here is the sample code:
String dateofbirth = "09-10-2010"; //date from jcalender
SimpleDateFormat myFormat = new SimpleDateFormat("dd-MM-yyyy");
// converting to Date object
Date date = myFormat.parse(dateofbirth);
SimpleDateFormat myFormat1 = new SimpleDateFormat("dd-MMM-yyyy");
// converting Date object to new format
String formattedDate = myFormat1.format(date);
System.out.println(formattedDate); // prints: 09-Oct-2010
// Before Java8 , No thread safe , has to import multiple packages(util, text), throws checked Exception(ParseException)
System.out.println(oldDate); //21 Jul 2019
SimpleDateFormat oldPattern = new SimpleDateFormat("dd MMM yyyy");
SimpleDateFormat newPattern = new SimpleDateFormat("dd-MM-yyyy");
try {
Date date = oldPattern.parse(oldDate);
String newDate = newPattern.format(date);
System.out.println(newDate); //21-07-2019
} catch (ParseException e) {
// Exception handling message/mechanism/logging as per company standard
}
// In Java8, Thread Safe, Immutable, throws unchecked Exception(DateTimeParseException)
DateTimeFormatter oldPattern8 = DateTimeFormatter.ofPattern("dd MMM-yyyy");
DateTimeFormatter newPattern8 = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate datetime = LocalDate.parse(oldDate, oldPattern8);
String output = datetime.format(newPattern8);
System.out.println(output); //21-07-2019

Code for matching date Pattern [duplicate]

This question already has answers here:
Java: Check the date format of current string is according to required format or not [duplicate]
(8 answers)
Closed 6 years ago.
I have a scenario where I get the date value from the xml and the date value from the website. I am able to retrieve the results, But now I need to compare the format of the retrieved results whether it is in 'MM YYYY' format or not ? How can i do that ? i have to check the format in String manner ie for e.g Apr 2010 but not 04 2010
Please can anyone of you help me ??
public class SimpleDate {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
String strDate = sdf.format(date);
sdf = new SimpleDateFormat("MMM/yyyy");
strDate = sdf.format(date);
System.out.println(strDate);
}
}
You can use a regular expresson, like this (Jan|Feb|Mar...) d{4}

How to format date string in java? [duplicate]

This question already has answers here:
Parse String to Date with Different Format in Java
(10 answers)
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Android Studio Convert ISO string to "America/New_York" when adding to event to calendar
(1 answer)
Change date format in a Java string
(22 answers)
Closed 9 years ago.
Hi i have the following string:2012-05-20T09:00:00.000Z
and i want to format it to be like 20/05/2012, 9am
How to do so in java?
Thanks
If you are looking for a solution to your particular case, it would be:
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse("2012-05-20T09:00:00.000Z");
String formattedDate = new SimpleDateFormat("dd/MM/yyyy, Ka").format(date);
use SimpleDateFormat to first parse() String to Date and then format() Date to String
package newpckg;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class StrangeDate {
public static void main(String[] args) {
// string containing date in one format
// String strDate = "2012-05-20T09:00:00.000Z";
String strDate = "2012-05-20T09:00:00.000Z";
try {
// create SimpleDateFormat object with source string date format
SimpleDateFormat sdfSource = new SimpleDateFormat(
"yyyy-MM-dd'T'hh:mm:ss'.000Z'");
// parse the string into Date object
Date date = sdfSource.parse(strDate);
// create SimpleDateFormat object with desired date format
SimpleDateFormat sdfDestination = new SimpleDateFormat(
"dd/MM/yyyy, ha");
// parse the date into another format
strDate = sdfDestination.format(date);
System.out
.println("Date is converted from yyyy-MM-dd'T'hh:mm:ss'.000Z' format to dd/MM/yyyy, ha");
System.out.println("Converted date is : " + strDate.toLowerCase());
} catch (ParseException pe) {
System.out.println("Parse Exception : " + pe);
}
}
}

Categories