new SimpleDateFormat(“hh:mm a”, Locale.getDefault()).parse(“04:30 PM”) giving Unparseable exception
Strange things happen at most desperate times.
I'm experiencing a bit strange thing in this line of code
Date time = new SimpleDateFormat("hh:mm a", Locale.getDefault()).parse("04:30 PM");
is giving
"Unparseable date: "04:30 PM" (at offset 6)"
exception in Android 6.0 and Android 6.0.1 devices only, in production app (on Google Play Store).
PS: I'm unable to regenerate this bug on Android 6.0 Emulator & HTC Desire 10 Pro Android 6.0.1.
Any help to regenerate this bug locally or how to way around it will be appreciated.
Edited: 15-Nov-2018
Devices giving this exception.
OS Version: 3.4.0-10662519 (G900FXXS1CQD8)
OS API Level: 23
Device: klte
Model (and Product): SM-G900F (kltexx)
Manufacturer: samsung
Other TAGS: release-keys
SD Card state: mounted
http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0.1; SM-G900F Build/MMB29M)
OS Version: 3.10.84 (v1AJW-0)
OS API Level: 23
Device: idol4
Model (and Product): 6055K (6055K)
Manufacturer: TCL
Other TAGS: release-keys
SD Card state: mounted
http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0.1; 6055K Build/MMB29M)
OS Version: 3.10.84-g05b37ae (16293194481ff)
OS API Level: 23
Device: p1
Model (and Product): LG-H818 (p1_global_com)
Manufacturer: LGE
Other TAGS: release-keys
SD Card state: mounted
http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0; LG-H818 Build/MRA58K)
java android time simpledateformat datetime-parsing
add a comment |
Strange things happen at most desperate times.
I'm experiencing a bit strange thing in this line of code
Date time = new SimpleDateFormat("hh:mm a", Locale.getDefault()).parse("04:30 PM");
is giving
"Unparseable date: "04:30 PM" (at offset 6)"
exception in Android 6.0 and Android 6.0.1 devices only, in production app (on Google Play Store).
PS: I'm unable to regenerate this bug on Android 6.0 Emulator & HTC Desire 10 Pro Android 6.0.1.
Any help to regenerate this bug locally or how to way around it will be appreciated.
Edited: 15-Nov-2018
Devices giving this exception.
OS Version: 3.4.0-10662519 (G900FXXS1CQD8)
OS API Level: 23
Device: klte
Model (and Product): SM-G900F (kltexx)
Manufacturer: samsung
Other TAGS: release-keys
SD Card state: mounted
http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0.1; SM-G900F Build/MMB29M)
OS Version: 3.10.84 (v1AJW-0)
OS API Level: 23
Device: idol4
Model (and Product): 6055K (6055K)
Manufacturer: TCL
Other TAGS: release-keys
SD Card state: mounted
http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0.1; 6055K Build/MMB29M)
OS Version: 3.10.84-g05b37ae (16293194481ff)
OS API Level: 23
Device: p1
Model (and Product): LG-H818 (p1_global_com)
Manufacturer: LGE
Other TAGS: release-keys
SD Card state: mounted
http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0; LG-H818 Build/MRA58K)
java android time simpledateformat datetime-parsing
Try to add current date this should work.
– Pawan Chaurasiya
Nov 15 '18 at 6:24
As an aside consider throwing away the long outmoded and notoriously troublesomeSimpleDateFormat
and friends, and adding ThreeTenABP to your Android project in order to usejava.time
, the modern Java date and time API. It is so much nicer to work with.
– Ole V.V.
Nov 15 '18 at 11:44
@PawanChaurasiya There should be no need for that and also it’s most likely not to solve the asker’s issue.
– Ole V.V.
Nov 15 '18 at 12:11
add a comment |
Strange things happen at most desperate times.
I'm experiencing a bit strange thing in this line of code
Date time = new SimpleDateFormat("hh:mm a", Locale.getDefault()).parse("04:30 PM");
is giving
"Unparseable date: "04:30 PM" (at offset 6)"
exception in Android 6.0 and Android 6.0.1 devices only, in production app (on Google Play Store).
PS: I'm unable to regenerate this bug on Android 6.0 Emulator & HTC Desire 10 Pro Android 6.0.1.
Any help to regenerate this bug locally or how to way around it will be appreciated.
Edited: 15-Nov-2018
Devices giving this exception.
OS Version: 3.4.0-10662519 (G900FXXS1CQD8)
OS API Level: 23
Device: klte
Model (and Product): SM-G900F (kltexx)
Manufacturer: samsung
Other TAGS: release-keys
SD Card state: mounted
http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0.1; SM-G900F Build/MMB29M)
OS Version: 3.10.84 (v1AJW-0)
OS API Level: 23
Device: idol4
Model (and Product): 6055K (6055K)
Manufacturer: TCL
Other TAGS: release-keys
SD Card state: mounted
http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0.1; 6055K Build/MMB29M)
OS Version: 3.10.84-g05b37ae (16293194481ff)
OS API Level: 23
Device: p1
Model (and Product): LG-H818 (p1_global_com)
Manufacturer: LGE
Other TAGS: release-keys
SD Card state: mounted
http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0; LG-H818 Build/MRA58K)
java android time simpledateformat datetime-parsing
Strange things happen at most desperate times.
I'm experiencing a bit strange thing in this line of code
Date time = new SimpleDateFormat("hh:mm a", Locale.getDefault()).parse("04:30 PM");
is giving
"Unparseable date: "04:30 PM" (at offset 6)"
exception in Android 6.0 and Android 6.0.1 devices only, in production app (on Google Play Store).
PS: I'm unable to regenerate this bug on Android 6.0 Emulator & HTC Desire 10 Pro Android 6.0.1.
Any help to regenerate this bug locally or how to way around it will be appreciated.
Edited: 15-Nov-2018
Devices giving this exception.
OS Version: 3.4.0-10662519 (G900FXXS1CQD8)
OS API Level: 23
Device: klte
Model (and Product): SM-G900F (kltexx)
Manufacturer: samsung
Other TAGS: release-keys
SD Card state: mounted
http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0.1; SM-G900F Build/MMB29M)
OS Version: 3.10.84 (v1AJW-0)
OS API Level: 23
Device: idol4
Model (and Product): 6055K (6055K)
Manufacturer: TCL
Other TAGS: release-keys
SD Card state: mounted
http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0.1; 6055K Build/MMB29M)
OS Version: 3.10.84-g05b37ae (16293194481ff)
OS API Level: 23
Device: p1
Model (and Product): LG-H818 (p1_global_com)
Manufacturer: LGE
Other TAGS: release-keys
SD Card state: mounted
http.agent = Dalvik/2.1.0 (Linux; U; Android 6.0; LG-H818 Build/MRA58K)
java android time simpledateformat datetime-parsing
java android time simpledateformat datetime-parsing
edited Nov 15 '18 at 12:09
Ole V.V.
32.3k74257
32.3k74257
asked Nov 15 '18 at 6:22
Ammar TahirAmmar Tahir
187318
187318
Try to add current date this should work.
– Pawan Chaurasiya
Nov 15 '18 at 6:24
As an aside consider throwing away the long outmoded and notoriously troublesomeSimpleDateFormat
and friends, and adding ThreeTenABP to your Android project in order to usejava.time
, the modern Java date and time API. It is so much nicer to work with.
– Ole V.V.
Nov 15 '18 at 11:44
@PawanChaurasiya There should be no need for that and also it’s most likely not to solve the asker’s issue.
– Ole V.V.
Nov 15 '18 at 12:11
add a comment |
Try to add current date this should work.
– Pawan Chaurasiya
Nov 15 '18 at 6:24
As an aside consider throwing away the long outmoded and notoriously troublesomeSimpleDateFormat
and friends, and adding ThreeTenABP to your Android project in order to usejava.time
, the modern Java date and time API. It is so much nicer to work with.
– Ole V.V.
Nov 15 '18 at 11:44
@PawanChaurasiya There should be no need for that and also it’s most likely not to solve the asker’s issue.
– Ole V.V.
Nov 15 '18 at 12:11
Try to add current date this should work.
– Pawan Chaurasiya
Nov 15 '18 at 6:24
Try to add current date this should work.
– Pawan Chaurasiya
Nov 15 '18 at 6:24
As an aside consider throwing away the long outmoded and notoriously troublesome
SimpleDateFormat
and friends, and adding ThreeTenABP to your Android project in order to use java.time
, the modern Java date and time API. It is so much nicer to work with.– Ole V.V.
Nov 15 '18 at 11:44
As an aside consider throwing away the long outmoded and notoriously troublesome
SimpleDateFormat
and friends, and adding ThreeTenABP to your Android project in order to use java.time
, the modern Java date and time API. It is so much nicer to work with.– Ole V.V.
Nov 15 '18 at 11:44
@PawanChaurasiya There should be no need for that and also it’s most likely not to solve the asker’s issue.
– Ole V.V.
Nov 15 '18 at 12:11
@PawanChaurasiya There should be no need for that and also it’s most likely not to solve the asker’s issue.
– Ole V.V.
Nov 15 '18 at 12:11
add a comment |
1 Answer
1
active
oldest
votes
Offset 6 of your string is where it says PM
.
It’s a locale issue. AM
and PM
, although derived from Latin are called that in English, not in very many other languages. Therefore those abbreviations are not recognized in very many locales. In your code you use Locale.getDefault()
, and if it returns a non-English-speaking locale, you are likely to get the error. Try for example Locale.ENGLISH
instead. Alternatively make sure you get a string in the right format and language for the default locale.
java.time
If you are doing any considerable work with times or dates in your app, and also for anyone programming for Java 8 or later or for Android API level 26 or higher: The classes you use, Date
and SimpleDateFormat
, have always had design problems, the latter in particular is typically troublesome. Fortunately both are long outdated now and replaced by java.time, the modern Java date and time API. So use this instead:
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
LocalTime time = LocalTime.parse("04:30 PM", timeFormatter);
System.out.println(time);
Output:
16:30
A LocalTime
is a time of day without date and without time zone and seems to match your need much better than the old-fashioned Date
class.
Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links
Oracle tutorial: Date Time explaining how to usejava.time
.
Java Specification Request (JSR) 310, wherejava.time
was first described.
ThreeTen Backport project, the backport ofjava.time
to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313558%2fnew-simpledateformathhmm-a-locale-getdefault-parse0430-pm-giving-un%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Offset 6 of your string is where it says PM
.
It’s a locale issue. AM
and PM
, although derived from Latin are called that in English, not in very many other languages. Therefore those abbreviations are not recognized in very many locales. In your code you use Locale.getDefault()
, and if it returns a non-English-speaking locale, you are likely to get the error. Try for example Locale.ENGLISH
instead. Alternatively make sure you get a string in the right format and language for the default locale.
java.time
If you are doing any considerable work with times or dates in your app, and also for anyone programming for Java 8 or later or for Android API level 26 or higher: The classes you use, Date
and SimpleDateFormat
, have always had design problems, the latter in particular is typically troublesome. Fortunately both are long outdated now and replaced by java.time, the modern Java date and time API. So use this instead:
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
LocalTime time = LocalTime.parse("04:30 PM", timeFormatter);
System.out.println(time);
Output:
16:30
A LocalTime
is a time of day without date and without time zone and seems to match your need much better than the old-fashioned Date
class.
Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links
Oracle tutorial: Date Time explaining how to usejava.time
.
Java Specification Request (JSR) 310, wherejava.time
was first described.
ThreeTen Backport project, the backport ofjava.time
to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
add a comment |
Offset 6 of your string is where it says PM
.
It’s a locale issue. AM
and PM
, although derived from Latin are called that in English, not in very many other languages. Therefore those abbreviations are not recognized in very many locales. In your code you use Locale.getDefault()
, and if it returns a non-English-speaking locale, you are likely to get the error. Try for example Locale.ENGLISH
instead. Alternatively make sure you get a string in the right format and language for the default locale.
java.time
If you are doing any considerable work with times or dates in your app, and also for anyone programming for Java 8 or later or for Android API level 26 or higher: The classes you use, Date
and SimpleDateFormat
, have always had design problems, the latter in particular is typically troublesome. Fortunately both are long outdated now and replaced by java.time, the modern Java date and time API. So use this instead:
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
LocalTime time = LocalTime.parse("04:30 PM", timeFormatter);
System.out.println(time);
Output:
16:30
A LocalTime
is a time of day without date and without time zone and seems to match your need much better than the old-fashioned Date
class.
Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links
Oracle tutorial: Date Time explaining how to usejava.time
.
Java Specification Request (JSR) 310, wherejava.time
was first described.
ThreeTen Backport project, the backport ofjava.time
to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
add a comment |
Offset 6 of your string is where it says PM
.
It’s a locale issue. AM
and PM
, although derived from Latin are called that in English, not in very many other languages. Therefore those abbreviations are not recognized in very many locales. In your code you use Locale.getDefault()
, and if it returns a non-English-speaking locale, you are likely to get the error. Try for example Locale.ENGLISH
instead. Alternatively make sure you get a string in the right format and language for the default locale.
java.time
If you are doing any considerable work with times or dates in your app, and also for anyone programming for Java 8 or later or for Android API level 26 or higher: The classes you use, Date
and SimpleDateFormat
, have always had design problems, the latter in particular is typically troublesome. Fortunately both are long outdated now and replaced by java.time, the modern Java date and time API. So use this instead:
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
LocalTime time = LocalTime.parse("04:30 PM", timeFormatter);
System.out.println(time);
Output:
16:30
A LocalTime
is a time of day without date and without time zone and seems to match your need much better than the old-fashioned Date
class.
Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links
Oracle tutorial: Date Time explaining how to usejava.time
.
Java Specification Request (JSR) 310, wherejava.time
was first described.
ThreeTen Backport project, the backport ofjava.time
to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Offset 6 of your string is where it says PM
.
It’s a locale issue. AM
and PM
, although derived from Latin are called that in English, not in very many other languages. Therefore those abbreviations are not recognized in very many locales. In your code you use Locale.getDefault()
, and if it returns a non-English-speaking locale, you are likely to get the error. Try for example Locale.ENGLISH
instead. Alternatively make sure you get a string in the right format and language for the default locale.
java.time
If you are doing any considerable work with times or dates in your app, and also for anyone programming for Java 8 or later or for Android API level 26 or higher: The classes you use, Date
and SimpleDateFormat
, have always had design problems, the latter in particular is typically troublesome. Fortunately both are long outdated now and replaced by java.time, the modern Java date and time API. So use this instead:
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
LocalTime time = LocalTime.parse("04:30 PM", timeFormatter);
System.out.println(time);
Output:
16:30
A LocalTime
is a time of day without date and without time zone and seems to match your need much better than the old-fashioned Date
class.
Question: Can I use java.time on Android?
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links
Oracle tutorial: Date Time explaining how to usejava.time
.
Java Specification Request (JSR) 310, wherejava.time
was first described.
ThreeTen Backport project, the backport ofjava.time
to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
answered Nov 15 '18 at 12:03
Ole V.V.Ole V.V.
32.3k74257
32.3k74257
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313558%2fnew-simpledateformathhmm-a-locale-getdefault-parse0430-pm-giving-un%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try to add current date this should work.
– Pawan Chaurasiya
Nov 15 '18 at 6:24
As an aside consider throwing away the long outmoded and notoriously troublesome
SimpleDateFormat
and friends, and adding ThreeTenABP to your Android project in order to usejava.time
, the modern Java date and time API. It is so much nicer to work with.– Ole V.V.
Nov 15 '18 at 11:44
@PawanChaurasiya There should be no need for that and also it’s most likely not to solve the asker’s issue.
– Ole V.V.
Nov 15 '18 at 12:11