Parsing string to date with timezone in national format - java

I try to parse string (russian locale) "01 августа 2014, пятница. 20:00 МСК" to java.util.Date. I try this code:
String dateString = "01 августа 2014, пятница. 20:00 МСК"
Locale rusLocale = new Locale.Builder().setLanguage("ru").setScript("Cyrl").build();
String pattern = "dd MMMM yyyy, EEEE. HH:mm z"
Date date = SimpleDateFormat(pattern, rusLocale).parse(dateString)
With month and weekday this code work fine, but when I try to parse string with timezone name МСК I get java.text.ParseException: Unparseable date. When I change MCK to MSK "01 августа 2014, пятница. 20:00 MSK" code also work fine. So we can parse strings month and weekday, but can't do it with timezone or "MCK" is just not valid?

Try this code. I think it is something relevant with your code.
String dateString = "17 октябрь 2014, пятница. 20:00";
Locale rusLocale = new Locale.Builder().setLanguage("ru").setScript("Cyrl").build();
String pattern = "dd MMMM yyyy, EEEE. HH:mm";
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, rusLocale);
dateFormat.setTimeZone(TimeZone.getTimeZone("МСК"));
Date date = dateFormat.parse(dateString);
} catch (ParseException e) {
...

Related

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

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

Parse String Date time in java

I have a date time (which is a string) in the following format: 2/19/2015 5:25:35 p.m, and I wanted to turn it in the following Date Format: Thu Feb 19 5:25:35 p.m. CET 2015 I tried the following code:
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
SimpleDateFormat format = new SimpleDateFormat ("ddd d mmm yyyy HH: mm: ss");
Date date = format.parse (sDatePrecedenteExecution)
but I got the following error:
java.text.ParseException: unparseable Date: "2/19/2015 5:30:29 p.m."
Has java.text.DateFormat.parse (DateFormat.java:337)
You are currently using the "output" format to read your incoming date string (2/19/2015 5:25:35 p.m), which is why you see the error.
You need to specify a second format for parsing your incoming date string, and use that format to parse instead. It should look like this:
SimpleDateFormat inFormat = new SimpleDateFormat ("dd/MM/yyyy HH:mm:ss")
Date date = inFormat.parse(sDatePrecedenteExecution)
Note that you also have a bug in your output format - m means minutes, and you want MMM, which is months. Have a look at the docs.
Your SimpleDateFormat doesn't match the format which you are entering. They should reflect the same.
Try this code
String parseDate = ""19/02/2015 17:30:29";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date parsedDate = dateFormat.parse(parseDate);
You need to change your code something like...
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
SimpleDateFormat format = new SimpleDateFormat ("dd/mm/yyyy HH:mm:ss");
try {
Date date = format.parse (sDatePrecedenteExecution);
System.out.println(date);
format = new SimpleDateFormat ("ddd d mmm yyyy HH: mm: ss");
String str = format.format(date);
System.out.println(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Try this pattern:
SimpleDateFormat format = new SimpleDateFormat ("dd mm yyyy HH:mm:ss");
You went wrong when you made: "ddd d mmm yyyy HH: mm: ss"
Use this pattern "dd/M/yyyy HH:mm:ss" instead & read the documentation
SimpleDateFormat format = new SimpleDateFormat ("dd/M/yyyy HH:mm:ss");
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
try{date =format.parse (sDatePrecedenteExecution);
}catch(Exception ex){//deal with it here}
System.out.println(date.toString()); //Thu Feb 19 17:30:29 UTC 2015

Convert string to date Android

I'm trying to convert a String that represents a date stored in SQLITE.
The date was stored into sqlite as follows:
Date date;
date.toString();
According with Java documentation, toString() method:
Returns a string representation of this Date. The formatting is
equivalent to using a SimpleDateFormat with the format string "EEE MMM
dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00
PDT 1999". The current default time zone and locale are used. If you
need control over the time zone or locale, use SimpleDateFormat
instead.
Until here, it's fine but, when I try to get the String and convert it to date again, Java throws an exception.
The String comes from sqlite:
Mon Jan 20 18:26:25 BRT 2014
So, I do:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date date= sdf.parse("Mon Jan 20 18:26:25 BRT 2014");
What I'm doing wrong?
Thanks.
try this code
String dateString = "here your date";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
Try this:
String w = "Mon Jan 20 18:26:25 BRT 2014";
SimpleDateFormat pre = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try{
Date date = pre.parse(w);
System.out.println(sdf.format(date));
}catch(Exception e){
e.printStackTrace();
}
Output:
20/01/2014
Formatter for storing and restoring data value in format dd/MM/yyyy
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Storing data
String dataAsString = simpleDateFormat.format(date); // 20/01/2014
Restoring data
Date data = simpleDateFormat.parse(dataAsString);

Java. Cant parse date using DateFormat

I have a date string:
Thu Feb 20 08:00:00 EET 1992
And using this code to format it:
String datePatternFrom = "EEE MMM dd HH:mm:ss ZZZ yyyy";
String datePatternTo = "MMM dd, yyyy";
String prettyDate = "";
try {
DateFormat fromFormatter = new SimpleDateFormat(datePatternFrom);
Date date = (Date)fromFormatter.parse(userBirthday.toString());
DateFormat toFormatter = new SimpleDateFormat(datePatternTo);
prettyDate = toFormatter.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Why I'am getting the exception?
java.text.ParseException: Unparseable date: "Thu Feb 20 08:00:00 EET 1992" (at offset 0)
The problem is with the weekday and month and your locale.
Thu is English, so you have to tell the parser that it should use English weekdays:
DateFormat fromFormatter = new SimpleDateFormat(datePatternFrom, Locale.US);
This will work for your pattern.
If you do not specify a locale, the default will be used, which is not always an English one. ;-)
It could be your locale. Try making a SDF with datePatternFrom, give it a date to format and print that somewhere. See what pops up.
Probably your userBirthday object was not created as a java.util.Date object. Can you try a System.out.println(userBirthday.getClass().getName());?

Categories