Java Unparseable Date Exception - java

I am trying to replace the hyphens with a forward slash, but it results to an unparseable date exception
String test = "2014-04-01 05:00:00";
Date date = new SimpleDateFormat("YYYY/MM/dd hh:mm:ss", Locale.ENGLISH).parse(test);
System.out.println(date);
I have the necessary values for it to be converted, can someone tell me why it returns an error? Plus I wanted to append an am/pm marker at the end of the format, is this possible?

You need to parse String to Date first in correct format as input String
yyyy-MM-dd HH:mm:ss
then you can use format() to print it in other format
yyyy/MM/dd hh:mm:ss
and don't expect toString() method of Date class return formatted value, it is fixed implementation

From SimpleDateFormat:
Letter Date or Time Component <br />
y Year <br />
Y Week year
H Hour in day (0-23)
h Hour in am/pm (1-12)
So, use yyyy for year and HH for hours in the day. Also, you're separating the fields by -, not by /:
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(test);
After doing this, as #JigarJoshi suspects, you can format your Date to look with another format:
String dateInDesiredFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss a", Locale.ENGLISH).format(date);
Or written as a complete block of code:
DateFormat parse = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH);
DateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss a", Locale.ENGLISH);
String test = "2014-04-01 05:00:00";
Date date = parse.parse(test);
System.out.println(format.format(date));
Produces the following output:
2014/04/01 05:00:00 AM

String test = "2014-04-01 05:00:00";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
Date oldDate = formatter.parse(test);
formatter.applyPattern("yyyy/MM/dd HH:mm:ss a");
Date newDate = formatter.parse(formatter.format(oldDate));
System.out.println(formatter.format(newDate));

Related

Convert date from yyyy-mm-dd to utc format

I have a date, "2015-06-08". I want convert it to "08 June 2015"
String oldDateString = "2015-06-08";
SimpleDateFormat oldDateFormat = new SimpleDateFormat("yyyy-mm-dd", Locale.getDefault());
SimpleDateFormat newDateFormat = new SimpleDateFormat("dd MMMM yyyy", Locale.getDefault());
Date date = oldDateFormat.parse(oldDateString);
String result = newDateFormat.format(date);
but this does not work correctly. It returns "08 January 2015". What have I done wrong?
Use yyyy-MM-dd insted yyyy-mm-dd because 'm' in lowercase is minutes.
Documentation : link

format date using SimpleDateFormat

I am getting parser Exception while parsing the below date using simple date format API.
String inputTimeStamp = "2012/07/19 09:49:00 - GMT -08:00";
java.text.SimpleDateFormat dateformate= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
effDate = dateformate.parse(inputTimeStamp);
Please help me out on this.
Change
java.text.SimpleDateFormat dateformate=
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
to
java.text.SimpleDateFormat dateformate=
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
You have slashes (/) in your inputTimeStamp
Because you are parsing a date which is in a different format then you have described it to the SimpleDateFormat. If effDate is of type String and should hold the formatted date, the following code might solve it, although there is not supposed to be an space between GMTand -08:00 .
String inputTimeStamp = "2012/07/19 09:49:00 - GMT -08:00";
SimpleDateFormat inputDateformat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss - z");
SimpleDateFormat dateformate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
effDate = dateformate.format(inputDateFormat.parse(inputTimeStamp));
I would highly recommend also joda time, especially when you want to do calculations on the date.
String inputTimeStamp = "2012/07/19 09:49:00 - GMT -08:00";
DateTimeFormatter inputDateformat = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss - z Z");
DateTimeFormatter dateformate = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
effDate = dateformate.print(inputDateFormat.parseDateTime(inputTimeStamp));
The pattern would be: yyyy/MM/dd HH:mm:ss - z where z is for General Time Zone. SimpleDateFormat.
Try this:
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss - z");
See this FYR

How to convert date and time stored in string to 24 format time in android?

I have my date and time stored in string i.e my string contains str ="18/01/2013 5:00:00 pm". How can I convert it to 24 format time in android?
You can use two SimpleDateFormat instances: one to parse the input as a date and a second to format the date as a string with the desired format.
For example, formattedDate in the code below will be 18/01/2013 17:00:00:
String str = "18/01/2013 5:00:00 pm";
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date dt = input.parse(str);
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDate = output.format(dt); //contains 18/01/2013 17:00:00
Notes:
hh is for Hour in am/pm (1-12) whereas HH is for Hour in day (0-23).
for more formatting options, check the javadoc
try
String str ="18/01/2013 5:00:00 pm";
Date date = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a").parse(str);
str = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(date);
System.out.println(str);
output
18/01/2013 17:00:00
To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.
Note: HH is for 24 hour and hh is for 12 hour date format
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
Example
String date = "18/01/2013 5:00:00 pm";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
You can use SimpleDateFormat for that:
SimpleDateFormat dateFormat = new SimpleDateFormat(dd/mm/yyyy HH:mm:ss");
Refer to this which was opposite to your requirement. Just posted the link so you might get the idea of difference that HH and hh makes.
Try using the java SimpleDateFormat class.
Example:
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
df.parse(date);
The upper case HH use a 24h format

Java - Date constructor accepts a Date String, But deprecated. Tried alternatives, but no luck

String temp_date="07/28/2011 11:06:37 AM";
Date date = new Date(temp_date); //Depricated
SimpleDateFormat sdf = new SimpleDateFormat("MMM-dd-yyyy hh:mm:ss");
String comp_date= sdf.format(date);
System.out.println(comp_date);
This works, But If I use something like this
String temp_date="07/28/2011 11:06:37 AM";
try{
SimpleDateFormat sdf = new SimpleDateFormat("MMM-dd-yyyy hh:mm:ss");
Date comp_date= sdf.parse(temp_date);
System.out.println(comp_date);
}catch(Exception e){
System.out.println(e);
}
This exception is thrown:
java.text.ParseException: Unparseable date: "07/28/2011 11:06:37 AM"
Your parsing pattern is wrong. It does not match the date string representation. The MMM denotes a 3-letter localized month abbreviation, while you have 2-digit month number in your actual date, you need MM. You've also slashes / as date/month/year separator and not -. For the AM/PM marker you also need an a afterwards so that the right hh can be parsed.
This should work:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
For an explanation of those patterns, read the SimpleDateFormat javadoc.
I believe that your concrete functional requirement is to convert the given date string as specified by the pattern MM/dd/yyyy hh:mm:ss a into another date string format, as specified by the pattern MMM-dd-yyyy hh:mm:ss. In that case, you should then have two SimpleDateFormat instances, one which parses the string in the given pattern to a Date and another which formats the parsed Date to the given pattern. This should do what you want:
String inputDate = "07/28/2011 11:06:37 AM";
Date date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a").parse(inputDate);
String outputDate = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss").format(date);
System.out.println(outputDate); // Jul-28-2011 11:06:37
Note that I changed hh in output to be HH because it would otherwise end up in 1-12 hour representation without an AM/PM marker. The HH represents it as 0-23 hour.
The format you gave the SimpleDateFormat uses - between the month, date, and year. Your string uses slashes.
At first look, it looks as if your format string is wrong.
MMM -- You specified this for the month, but you aren't passing a 3 char month.
Try MM and see if that helps.
Take a look at this for some additional date format information:
http://www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm

How can I convert a String date with full month names to a Date object in java?

How can I convert dates with full month names into a Date object in Java? Here's a sample date that I need to convert: 6 December 2002.
Thanks.
String str = "6 December 2002";
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
Date date = (Date)formatter.parse(str);
Here is working IDE One demo
Must See
API Doc
Have a look at SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("d MMMMM yyyy", Locale.US);
Date d = sdf.parse("6 December 2002");
System.out.println(d);

Categories