Hello Please help me out, I have gone through many questions but didn't get a solution.
Code
String localDate1="Miércoles, 04 Octubre 2017 12:00 PM";
Locale spanishLocale=new Locale("es", "ES");
SimpleDateFormat spanishLocale1=new SimpleDateFormat(getString(R.string.jom_events_date_input_format_12_hrs),spanishLocale);
String dateInSpanish=spanishLocale1.parse(localDate1).toString();
Log.v("###WWW","in Spanish: "+dateInSpanish);
Error
java.text.ParseException: Unparseable date: "Miércoles, 04 Octubre 2017 12:00 PM" (at offset 33)
Just for the record:
You have fortunately posted your error message which points to the offset 33 (that is the position of "PM" in your input). So we can state:
Your problem is related to device-dependent localization data (or OS-dependent), here the concrete data for the Spanish representation of AM/PM. In old versions of CLDR-unicode repository (industry standard as common source for many Java-, C#- or Android distributions), the data "AM" and "PM" were used but in newer versions it uses "a. m." or "p. m." for Spanish.
So in case of mismatch between your input to be parsed (containing "PM") and the real i18n-data you have, I recommend as pragmatic solution string preprocessing:
String input = "Miércoles, 04 Octubre 2017 12:00 PM";
input = input.replace("PM", "p. m.");
// now parse your input with Spanish locale and the appropriate pattern
Please check you spelling on this line
String localDate1="Miércoles, 04 Octubre 2017 12:00 PM";
change October instead of Octubre and also check this Miércoles
You can use this code for your reference::
This code converts:---
miércoles, 04 octubre 2017 12:00 AM
to
Wed Oct 04 00:00:00 IST 2017
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class test {
public static void main(String[] args) throws IOException, ParseException {
//Wednesday, October 4, 2017
String dateInString = "4-Oct-2017";
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date date = formatter.parse(dateInString);
SimpleDateFormat formato = new SimpleDateFormat("EEEE, dd MMMM yyyy hh:mm aaaa", new Locale("es", "ES"));
String fecha = formato.format(date);
System.out.println(fecha);
String localDate1 = fecha;
Locale spanishLocale = new Locale("es", "ES");
String pattern = "E, dd MMMM yyyy hh:mm aaaa";
SimpleDateFormat spanishLocale1 = new SimpleDateFormat(pattern, spanishLocale);
String dateInSpanish = spanishLocale1.parse(localDate1).toString();
System.out.println(dateInSpanish);
}
}
Related
I have a value like the following Mon Jun 18 11:32:05 BST 2012 and I want to convert this to 18 June 2012
How to convert this?
Below is what i have tried..
public String getRequiredDate(String sendInputDate) {
Optional<OffsetDateTime> date = dateParsing(sendInputDate);
if(date) {
String REQUIRED_PATTERN="dd MMMM yyyy";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(REQUIRED_PATTERN);
return date.get().format(formatter);
}
}
import java.time.OffsetDateTime; hence returning in same
public static Optional<OffsetDateTime> dateParsing(String date) {
String PATTERN = "EEE MMM dd HH:mm:ss zzz yyyy";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN);
return Optional.of(OffsetDateTime.parse(date, formatter));
}
But its always returning me " " (blank)
It doesn't "return blank". You presumably have some exception handling somewhere which is swallowing the actual issue and replacing that with a blank string.
The actual issue is this exception:
java.time.format.DateTimeParseException: Text 'Mon Jun 18 11:32:05 BST 2012' could not be parsed:
Unable to obtain OffsetDateTime from TemporalAccessor
i.e. your string does not contain an offset, but you are trying to parse it into something which requires one. It has a zone.
You could parse it into a ZonedDateTime and then convert it, for example.
OffsetDateTime parsed = ZonedDateTime.parse(date, formatter).toOffsetDateTime();
I am getting the date in long format like this :
Wednesday, August 1, 2018
I want this in the below format:
Wed, Aug 01, 2018
I used the below code:
public static String getShortDate(Date date){
SimpleDateFormat format = new SimpleDateFormat("E, MMM dd, yyyy");
return format.format(date);
}
This works fine for en_US. But how to make it work for other locales
For example the long format for German is :
Samstag, 16. Juni 2018
how to get the above short format for it?
The getShortDate method parameter takes Date, but I can change it to String.
If the longer format is : Samstag, 16. Juni 2018
i need it to be : Sa., 16. Jun. 2018
i am using the below code :
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM, yyyy", locale);
return format.format(date);
This is giving me output as: Sa, 16 Jun, 2018
How to get that dot(.) after Sa
To get short date format in different locale code, you could try below code:
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("E, MMM dd, yyyy", new Locale("de", "de"));//for german germany
String date = simpleDateFormat.format(new Date());
Output:
Di, Mär 27, 2018
Consider this program that format the current date, and try to parse it again. It succeeds in French, but fails in English and I don't understand why.
import java.util.Locale;
import java.text.DateFormat;
import java.time.Instant;
import java.util.Date;
import java.text.SimpleDateFormat;
public class HelloWorld{
public static void main(String []args){
try{
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT, Locale.ENGLISH).parse(formatter.format(new Date()));
System.out.println("English - success");
}catch(Exception ex){
System.out.println(ex);
}
try{
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.FRENCH);
DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT, Locale.FRENCH).parse(formatter.format(new Date()));
System.out.println("French - success");
}catch(Exception ex){
System.out.println(ex);
}
System.out.println(Locale.getDefault());
}
}
Output:
java.text.ParseException: Unparseable date: "11 Feb 2015 11:09:26"
French -success
en_US
Please look at http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#parse%28java.lang.String,%20java.text.ParsePosition%29 before telling me that I should use a pattern or anything else. This method is meant to parse a String without a pattern.
I suspect your input format string is wrong.
As per documentation Jun 30, 2009 7:03:47 AM would be a valid format for en_US on Default settings.
You could always check if your format is right by formatting a given Date first.
For example
System.out.println(DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT, Locale.ENGLISH).format(new Date())); gives Feb 11, 2015 12:34:48 PM, which doesn't fit 11 Feb 2015 11:09:26.
This should be your correct formatted string for en_US parsing: Feb 11, 2015 11:09:26 AM. Remember, this is AM / PM 12 hour time format, which can be annoying.
are you sure that dd MMM yyyy HH:mm:ss is a correct english format dont they use month before days like US format ?
The default format for English is like this:
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a", Locale.ENGLISH);
This is the date format that I need to deal with
Wed Aug 21 2013 00:00:00 GMT-0700 (PDT)
But I don't get what the last two parts are. Is the GMT-0700 fixed? Should it be something like this?
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT-0700' (z)");
No, it is not fixed. It is a TimeZone. You can match it with Z in the date format.
To be more precise, in SimpleDateFormat formats :
Z matches the -0700 part.
GMT is fixed. Escape it with some quotes.
z matches the PDT part. (PDT = Pacific Daylight Time).
The parenthesis around PDT are fixed. Escape them with parenthesis.
You can parse your date with the following format :
EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('z')'
Another remark : Wed Aug contains the day and month in English so you must use an english locale with your SimpleDateFormat or the translation will fail.
new SimpleDateFormat("*format*", Locale.ENGLISH);
Here is the Javadoc:
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
For this example: Wed Aug 21 2013 00:00:00 GMT-0700 (PDT), you'd want this format:
import java.text.SimpleDateFormat;
import java.util.Date;
public class JavaDate {
public static void main (String[] args) throws Exception {
String s= "Wed Aug 21 2013 00:00:00 GMT-0700 (PDT)";
SimpleDateFormat sdf =
new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'z '('Z')'");
Date d = sdf.parse (s);
System.out.println ("Date=" + d + "...");
}
}
EXAMPLE OUTPUT: Date=Tue Aug 20 23:00:00 PDT 2013...
Thanx to Arnaud Denoyelle above for his edits!
How can I parse this date format Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest) to this date format 05-14-2010 I mean mm-dd-yyyy
it's telling me this error :
java.text.ParseException: Unparseable date: "Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest)"
EDIT
SimpleDateFormat formatter = new SimpleDateFormat("M-d-yyyy");
newFirstDate = formatter.parse(""+vo.getFirstDate()); //here the error
Thanks in advance!
This code first adapts the string a bit and then goes on to parse it. It respects the timezone, just removes "GMT" because that's how SimpleDateFormat likes it.
final String date = "Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest)"
.replaceFirst("GMT", "");
System.out.println(
new SimpleDateFormat("MM-dd-yyyy").format(
new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z").parse(date)));
Prints:
05-14-2010
Bear in mind that the output is also timezone-sensitive. The instant defined by your input string is being interpreted in my timezone as belonging to the date that the program printed. If you just need to transform "May 14 2010" into "05-14-2010", that's another story and SimpleDateFormat is not well suited for that. The JodaTime library would handle that case much more cleanly.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test
{
public static void main( String args[] ) throws ParseException
{
// Remove GMT from date string.
String string = "Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest)".replace( "GMT" , "" );
// Parse string to date object.
Date date = new SimpleDateFormat( "EEE MMM dd yyyy HH:mm:ss Z" ).parse( string );
// Format date to new format
System.out.println( new SimpleDateFormat( "MM-dd-yyyy" ).format( date ) );
}
}
Outputs:
05-13-2010