Why is the date unparseable [duplicate] - java

This question already has answers here:
java.text.ParseException: Unparseable date
(10 answers)
Closed 3 years ago.
I'm attempting to parse a new Date object, but I keep hitting the following error.
W/System.err: java.text.ParseException: Unparseable date: "Thu May 16 09:28:39 GMT+01:00 2019"
I've attempted various different patterns for dateFormat, but nothing seems to work.
This is where the error is.
c.setTime(dateFormat.parse(oldDate));
Code
public static String addDay(int numberOfDays) {
String oldDate = String.valueOf(new Date());
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss", Locale.ENGLISH);
Calendar c = Calendar.getInstance();
try {
c.setTime(dateFormat.parse(oldDate));
} catch (ParseException e) {
e.printStackTrace();
}
c.add(Calendar.DAY_OF_YEAR,numberOfDays);
dateFormat=new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss", Locale.ENGLISH);
Date newDate=new Date(c.getTimeInMillis());
String resultDate=dateFormat.format(newDate);
return resultDate;
}

Try This function
In your question you are converting Date to string
then after you are once again Parsing String to Date
that is very Long way. you can directly set as
c.setTime(oldDate);
public static String addDay(int numberOfDays) {
Date oldDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss", Locale.ENGLISH);
Calendar c = Calendar.getInstance();
c.setTime(oldDate);
c.add(Calendar.DAY_OF_YEAR,numberOfDays);
dateFormat=new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss", Locale.ENGLISH);
Date newDate=new Date(c.getTimeInMillis());
String resultDate=dateFormat.format(newDate);
return resultDate;
}

The pattern should be like :
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
Locale.ENGLISH);
Then to print you need a second SimpleDateFormat:
Date parsedDate = sdf.parse(date);
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
System.out.println(print.format(parsedDate));

Your pattern is wrong. You should use EEE MMM dd HH:mm:ss z yyyy

Related

convert date in android [duplicate]

This question already has answers here:
How to parse month full form string using DateFormat in Java?
(4 answers)
Closed 2 years ago.
I want to convert this date but parse exemption it should be like this result "2019-12-28 14:00:00"
try {
String strCurrentDate = "April 08 2020 4:24 AM"
SimpleDateFormat format = new SimpleDateFormat("MMM dd yyyy hh:mm a");
Date newDate = format.parse(strCurrentDate);
format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(newDate);
Log.d("datessscc", date);
} catch (ParseException e) {
e.printStackTrace();
}
Please change your SimpleDateFormat
Try This:
SimpleDateFormat input = new SimpleDateFormat("MMM dd yyyy hh:mm a");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date getAbbreviate=input.parse("May 22 2020 4:22 AM"); // parse input
Log.i("ouput_Date", "onCreate: "+output.format(getAbbreviate)); // format output
String date=output.format(getAbbreviate);
} catch (ParseException e) {
e.printStackTrace();
}

How can I reformat date and time?

How can I convert 24 hours time format into 12 hours format? I know this question has been asked many times, but my problem is different. My current time is:
Tue Nov 07 18:44:47 GMT+05:00 2017
I just want 6:44 pm from my date time. I tried this:
private void convertTime(String time)
{
try {
final SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
final Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm a").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}
}
final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Using hh will give you 12 hours format and HH 24 hour format. More details on documentation.
Edit:
Your initial format must be the following in order to parse your date string to a Date object:
final SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss 'GMT'Z yyyy");
final Date dateObj = sdf.parse(time);
After that you can format time to your needs.
From SimpleDateFormat documentation:
"h:mm a": 12:08 PM
So the format you need for:
I just want 6:44 pm from my date time
is:
final SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
Date date = sdf1.parse(time);
final SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
String newDateString = sdf.format(date);
try {
final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
final Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm a").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}

Parse yyyy-MM-DD String to Date yyyy-MM-DD in Android?

String startDateStr = "2017-02-03"
DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
2017-02-03 date is parsed to Tue Jan 03 00:00:00 GMT+05:45 2017
Did I
miss something?
Update
I needed a string to be converted to a date object
while maintaining the same format.
The reason for this is I want to make use of public boolean after(Date when) method
This will work ^_^
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
String startDateStr ="2017-02-03";
Date date = null;
try {
date = inputFormat.parse(startDateStr);
String startDateStrNewFormat = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Little explanation of your output :
D is Day in year (1-365)
d is day in month (1-31)
Check the document
Use SimpleDateFormat type for fomatter. You are creating DateFormat object but using SimpleDateFormat.
String startDateStr = "2017-02-03"
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
Yes you missed something. You used DD instead of dd in your yyyy-MM-DD format string. Here is how you do it:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(new Date());

How to format 2016-02-12T15:23:20+02:00 time [duplicate]

This question already has answers here:
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 7 years ago.
I'm getting the above Date format from our webservice. I have an idea on how to format the date, im just having issues with the fact it comes down as a string.
I have tried this but I need to return it as a String, which in a way isn't a problem.
This is what I have tried but it throws an Exception:
java.text.ParseException: Unparseable date:
"2016-02-26T00:00:00+02:00" (at offset 4)
Code:
public static String formatDate(String unFormattedTime) {
String formattedTime;
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM HH:mm");
Date date = sdf.parse(unFormattedTime);
formattedTime = sdf.format(date);
return formattedTime;
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}
How could I format it in a format like dd MMM HH:mm?
Here you are with a working snippet of what you want to achieve:
public class FormatDateExample {
public static void main(String[] args) {
String date = "2016-02-26T00:00:00+02:00";
System.out.println(formatDate(date));
}
public static String formatDate(String unFormattedTime) {
String formattedTime;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = sdf.parse(unFormattedTime);
sdf = new SimpleDateFormat("dd MMM HH:mm");
formattedTime = sdf.format(date);
return formattedTime;
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}
}
First you have to parse the date with the given format you have
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = sdf.parse(unFormattedTime);
Then you have to format that date to the desired format "dd MMM HH:mm"
sdf = new SimpleDateFormat("dd MMM HH:mm");
formattedTime = sdf.format(date);
If you are using Java 8 you can try this:
LocalDate parsedDate = LocalDate.parse(unFormattedTime, ISO_OFFSET_DATE_TIME)
Ref:
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_OFFSET_DATE_TIME

Getting the month and day from a TimeStamp

I have a String timeStamp in this format "Wed, 29 Jan 2014 20:14:15 GMT". I want to be able to compare this date with another date that is in this format 01/25/1999. I have tried simpledateformatter but with no success.
String a = connection.getHeaderField("Last-Modified"); //Returns Wed, 29 Jan 2014 20:14:15 GMT
Date lastModDate = new Date(file.lastModified()); //returns 01/25/1999
This is the simpleDateFormatter I tried implementing
SimpleDateFormat formatter;
Date dateIn = null;
formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
try{
dateIn = (Date)formatter.parse(a);
}catch(ParseException e){
e.printStackTrace();
}
Log.d(TAG, "The server date is formated to : " + dateIn);
The dateIn is always null.
I want the to be able to do something like this
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
Date strDate = sdf.parse(valid_until);
if (new Date().after(strDate)) {
}
Use the following code...you will get the problem right.
Calendar calender = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String timeStamp = formatter.format(calender.getTime());
Date strDate = formatter.parse(timeStamp);
String currentTimeStamp = formatter.format(new Date());
Date currentTime = formatter.parse(currentTimeStamp);
if (currentTime.after(strDate)) {
}
Don't know what you tried but this should work:
String a = connection.getHeaderField("Last-Modified");
Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH).parse(a);
This can help http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
The reason is that you are using the wrong date format for your formatter. If the date you receive looks like
"Wed, 29 Jan 2014 20:14:15 GMT"
Then you should use the following format
formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
You should use the Calendar class and its subclass GregorianCalendar. For exampe, to get the month of your date:
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.get(Calendar.MONTH);

Categories