JAVA GSON date converter : How to get midnight as 00:00 AM instead of 12:00 AM
I use GSON to get a Json with different data. Everything works fine but when the user type 00:30:00 AM as half past midnight, it is converted as 12:30:00 AM. I d like to have it as 00:30:00 AM.
Is there a way to do without changing anything else in date conversion ?
java json date gson
add a comment |
I use GSON to get a Json with different data. Everything works fine but when the user type 00:30:00 AM as half past midnight, it is converted as 12:30:00 AM. I d like to have it as 00:30:00 AM.
Is there a way to do without changing anything else in date conversion ?
java json date gson
Is GSON giving you the time as a string in that format? (Where I learnt the AM/PM system, 12:30 AM is correct for half past midnight, BTW.)
– Ole V.V.
Nov 26 '18 at 12:43
Yes it s a String format. and I d need all the hours between midnight and 1 am to be changed into 00:{the right minute}:00
– Jean
Nov 26 '18 at 13:32
add a comment |
I use GSON to get a Json with different data. Everything works fine but when the user type 00:30:00 AM as half past midnight, it is converted as 12:30:00 AM. I d like to have it as 00:30:00 AM.
Is there a way to do without changing anything else in date conversion ?
java json date gson
I use GSON to get a Json with different data. Everything works fine but when the user type 00:30:00 AM as half past midnight, it is converted as 12:30:00 AM. I d like to have it as 00:30:00 AM.
Is there a way to do without changing anything else in date conversion ?
java json date gson
java json date gson
edited Nov 26 '18 at 9:11
Jean
asked Nov 26 '18 at 9:05
JeanJean
14611
14611
Is GSON giving you the time as a string in that format? (Where I learnt the AM/PM system, 12:30 AM is correct for half past midnight, BTW.)
– Ole V.V.
Nov 26 '18 at 12:43
Yes it s a String format. and I d need all the hours between midnight and 1 am to be changed into 00:{the right minute}:00
– Jean
Nov 26 '18 at 13:32
add a comment |
Is GSON giving you the time as a string in that format? (Where I learnt the AM/PM system, 12:30 AM is correct for half past midnight, BTW.)
– Ole V.V.
Nov 26 '18 at 12:43
Yes it s a String format. and I d need all the hours between midnight and 1 am to be changed into 00:{the right minute}:00
– Jean
Nov 26 '18 at 13:32
Is GSON giving you the time as a string in that format? (Where I learnt the AM/PM system, 12:30 AM is correct for half past midnight, BTW.)
– Ole V.V.
Nov 26 '18 at 12:43
Is GSON giving you the time as a string in that format? (Where I learnt the AM/PM system, 12:30 AM is correct for half past midnight, BTW.)
– Ole V.V.
Nov 26 '18 at 12:43
Yes it s a String format. and I d need all the hours between midnight and 1 am to be changed into 00:{the right minute}:00
– Jean
Nov 26 '18 at 13:32
Yes it s a String format. and I d need all the hours between midnight and 1 am to be changed into 00:{the right minute}:00
– Jean
Nov 26 '18 at 13:32
add a comment |
2 Answers
2
active
oldest
votes
I would have preferred to persuade GSON to give me the desired format, but sorry, I don’t know GSON so cannot tell you whether that’s possible. However, if it isn’t, reformatting the string you get isn’t difficult when you know how:
DateTimeFormatter gsonTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
DateTimeFormatter desiredTineFormatter = DateTimeFormatter.ofPattern("KK:mm:ss a");
String timeStringFromGson = "12:30:00 AM";
LocalTime time = LocalTime.parse(timeStringFromGson, gsonTimeFormatter);
String formattedTime = time.format(desiredTineFormatter);
System.out.println(formattedTime);
Output from this snippet is:
00:30:00 AM
The format pattern letters hh (lowercase) and KK (uppercase) both mean hour within AM or PM, but the former goes from 01 through 12, the latter from 00 through 11, so gives you the desired hour, also in case the hour from GSON happened to be 12.
add a comment |
Set your date format when creating gson object from Gson builder.
Gson gson = new GsonBuilder().setDateFormat("HH:mm:ss").create();
I think you didn't get my question. or may I didnt get your answer but I dont see how setDateFormat(HH:mm) would get me a 00:30:00 AM instead of a 12:30:00 AM ?
– Jean
Nov 26 '18 at 9:13
1
@Jean your question is begging for bad answers, since you didn't post any code allowing us to understand, reproduce and fix the problem. Post a complete minimal example reproducing the problem.
– JB Nizet
Nov 26 '18 at 9:14
Addedssfor seconds. Just wonder why you need AM while the hour is in 00-23.
– htpvl
Nov 26 '18 at 9:15
The original code is not mine. That s why I want to touch the Json generated as less as possible. The Json never sends 12-24 hours. But the app using this Json generates 00-24 hours.
– Jean
Nov 26 '18 at 9:53
How would this answer treat, say,01:15:00 PM?
– Ole V.V.
Nov 26 '18 at 12:41
|
show 1 more 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%2f53477714%2fjava-gson-date-converter-how-to-get-midnight-as-0000-am-instead-of-1200-am%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would have preferred to persuade GSON to give me the desired format, but sorry, I don’t know GSON so cannot tell you whether that’s possible. However, if it isn’t, reformatting the string you get isn’t difficult when you know how:
DateTimeFormatter gsonTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
DateTimeFormatter desiredTineFormatter = DateTimeFormatter.ofPattern("KK:mm:ss a");
String timeStringFromGson = "12:30:00 AM";
LocalTime time = LocalTime.parse(timeStringFromGson, gsonTimeFormatter);
String formattedTime = time.format(desiredTineFormatter);
System.out.println(formattedTime);
Output from this snippet is:
00:30:00 AM
The format pattern letters hh (lowercase) and KK (uppercase) both mean hour within AM or PM, but the former goes from 01 through 12, the latter from 00 through 11, so gives you the desired hour, also in case the hour from GSON happened to be 12.
add a comment |
I would have preferred to persuade GSON to give me the desired format, but sorry, I don’t know GSON so cannot tell you whether that’s possible. However, if it isn’t, reformatting the string you get isn’t difficult when you know how:
DateTimeFormatter gsonTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
DateTimeFormatter desiredTineFormatter = DateTimeFormatter.ofPattern("KK:mm:ss a");
String timeStringFromGson = "12:30:00 AM";
LocalTime time = LocalTime.parse(timeStringFromGson, gsonTimeFormatter);
String formattedTime = time.format(desiredTineFormatter);
System.out.println(formattedTime);
Output from this snippet is:
00:30:00 AM
The format pattern letters hh (lowercase) and KK (uppercase) both mean hour within AM or PM, but the former goes from 01 through 12, the latter from 00 through 11, so gives you the desired hour, also in case the hour from GSON happened to be 12.
add a comment |
I would have preferred to persuade GSON to give me the desired format, but sorry, I don’t know GSON so cannot tell you whether that’s possible. However, if it isn’t, reformatting the string you get isn’t difficult when you know how:
DateTimeFormatter gsonTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
DateTimeFormatter desiredTineFormatter = DateTimeFormatter.ofPattern("KK:mm:ss a");
String timeStringFromGson = "12:30:00 AM";
LocalTime time = LocalTime.parse(timeStringFromGson, gsonTimeFormatter);
String formattedTime = time.format(desiredTineFormatter);
System.out.println(formattedTime);
Output from this snippet is:
00:30:00 AM
The format pattern letters hh (lowercase) and KK (uppercase) both mean hour within AM or PM, but the former goes from 01 through 12, the latter from 00 through 11, so gives you the desired hour, also in case the hour from GSON happened to be 12.
I would have preferred to persuade GSON to give me the desired format, but sorry, I don’t know GSON so cannot tell you whether that’s possible. However, if it isn’t, reformatting the string you get isn’t difficult when you know how:
DateTimeFormatter gsonTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
DateTimeFormatter desiredTineFormatter = DateTimeFormatter.ofPattern("KK:mm:ss a");
String timeStringFromGson = "12:30:00 AM";
LocalTime time = LocalTime.parse(timeStringFromGson, gsonTimeFormatter);
String formattedTime = time.format(desiredTineFormatter);
System.out.println(formattedTime);
Output from this snippet is:
00:30:00 AM
The format pattern letters hh (lowercase) and KK (uppercase) both mean hour within AM or PM, but the former goes from 01 through 12, the latter from 00 through 11, so gives you the desired hour, also in case the hour from GSON happened to be 12.
answered Nov 26 '18 at 14:21
Ole V.V.Ole V.V.
32.1k74257
32.1k74257
add a comment |
add a comment |
Set your date format when creating gson object from Gson builder.
Gson gson = new GsonBuilder().setDateFormat("HH:mm:ss").create();
I think you didn't get my question. or may I didnt get your answer but I dont see how setDateFormat(HH:mm) would get me a 00:30:00 AM instead of a 12:30:00 AM ?
– Jean
Nov 26 '18 at 9:13
1
@Jean your question is begging for bad answers, since you didn't post any code allowing us to understand, reproduce and fix the problem. Post a complete minimal example reproducing the problem.
– JB Nizet
Nov 26 '18 at 9:14
Addedssfor seconds. Just wonder why you need AM while the hour is in 00-23.
– htpvl
Nov 26 '18 at 9:15
The original code is not mine. That s why I want to touch the Json generated as less as possible. The Json never sends 12-24 hours. But the app using this Json generates 00-24 hours.
– Jean
Nov 26 '18 at 9:53
How would this answer treat, say,01:15:00 PM?
– Ole V.V.
Nov 26 '18 at 12:41
|
show 1 more comment
Set your date format when creating gson object from Gson builder.
Gson gson = new GsonBuilder().setDateFormat("HH:mm:ss").create();
I think you didn't get my question. or may I didnt get your answer but I dont see how setDateFormat(HH:mm) would get me a 00:30:00 AM instead of a 12:30:00 AM ?
– Jean
Nov 26 '18 at 9:13
1
@Jean your question is begging for bad answers, since you didn't post any code allowing us to understand, reproduce and fix the problem. Post a complete minimal example reproducing the problem.
– JB Nizet
Nov 26 '18 at 9:14
Addedssfor seconds. Just wonder why you need AM while the hour is in 00-23.
– htpvl
Nov 26 '18 at 9:15
The original code is not mine. That s why I want to touch the Json generated as less as possible. The Json never sends 12-24 hours. But the app using this Json generates 00-24 hours.
– Jean
Nov 26 '18 at 9:53
How would this answer treat, say,01:15:00 PM?
– Ole V.V.
Nov 26 '18 at 12:41
|
show 1 more comment
Set your date format when creating gson object from Gson builder.
Gson gson = new GsonBuilder().setDateFormat("HH:mm:ss").create();
Set your date format when creating gson object from Gson builder.
Gson gson = new GsonBuilder().setDateFormat("HH:mm:ss").create();
edited Nov 26 '18 at 9:14
answered Nov 26 '18 at 9:10
htpvlhtpvl
647310
647310
I think you didn't get my question. or may I didnt get your answer but I dont see how setDateFormat(HH:mm) would get me a 00:30:00 AM instead of a 12:30:00 AM ?
– Jean
Nov 26 '18 at 9:13
1
@Jean your question is begging for bad answers, since you didn't post any code allowing us to understand, reproduce and fix the problem. Post a complete minimal example reproducing the problem.
– JB Nizet
Nov 26 '18 at 9:14
Addedssfor seconds. Just wonder why you need AM while the hour is in 00-23.
– htpvl
Nov 26 '18 at 9:15
The original code is not mine. That s why I want to touch the Json generated as less as possible. The Json never sends 12-24 hours. But the app using this Json generates 00-24 hours.
– Jean
Nov 26 '18 at 9:53
How would this answer treat, say,01:15:00 PM?
– Ole V.V.
Nov 26 '18 at 12:41
|
show 1 more comment
I think you didn't get my question. or may I didnt get your answer but I dont see how setDateFormat(HH:mm) would get me a 00:30:00 AM instead of a 12:30:00 AM ?
– Jean
Nov 26 '18 at 9:13
1
@Jean your question is begging for bad answers, since you didn't post any code allowing us to understand, reproduce and fix the problem. Post a complete minimal example reproducing the problem.
– JB Nizet
Nov 26 '18 at 9:14
Addedssfor seconds. Just wonder why you need AM while the hour is in 00-23.
– htpvl
Nov 26 '18 at 9:15
The original code is not mine. That s why I want to touch the Json generated as less as possible. The Json never sends 12-24 hours. But the app using this Json generates 00-24 hours.
– Jean
Nov 26 '18 at 9:53
How would this answer treat, say,01:15:00 PM?
– Ole V.V.
Nov 26 '18 at 12:41
I think you didn't get my question. or may I didnt get your answer but I dont see how setDateFormat(HH:mm) would get me a 00:30:00 AM instead of a 12:30:00 AM ?
– Jean
Nov 26 '18 at 9:13
I think you didn't get my question. or may I didnt get your answer but I dont see how setDateFormat(HH:mm) would get me a 00:30:00 AM instead of a 12:30:00 AM ?
– Jean
Nov 26 '18 at 9:13
1
1
@Jean your question is begging for bad answers, since you didn't post any code allowing us to understand, reproduce and fix the problem. Post a complete minimal example reproducing the problem.
– JB Nizet
Nov 26 '18 at 9:14
@Jean your question is begging for bad answers, since you didn't post any code allowing us to understand, reproduce and fix the problem. Post a complete minimal example reproducing the problem.
– JB Nizet
Nov 26 '18 at 9:14
Added
ss for seconds. Just wonder why you need AM while the hour is in 00-23.– htpvl
Nov 26 '18 at 9:15
Added
ss for seconds. Just wonder why you need AM while the hour is in 00-23.– htpvl
Nov 26 '18 at 9:15
The original code is not mine. That s why I want to touch the Json generated as less as possible. The Json never sends 12-24 hours. But the app using this Json generates 00-24 hours.
– Jean
Nov 26 '18 at 9:53
The original code is not mine. That s why I want to touch the Json generated as less as possible. The Json never sends 12-24 hours. But the app using this Json generates 00-24 hours.
– Jean
Nov 26 '18 at 9:53
How would this answer treat, say,
01:15:00 PM?– Ole V.V.
Nov 26 '18 at 12:41
How would this answer treat, say,
01:15:00 PM?– Ole V.V.
Nov 26 '18 at 12:41
|
show 1 more 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%2f53477714%2fjava-gson-date-converter-how-to-get-midnight-as-0000-am-instead-of-1200-am%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
Is GSON giving you the time as a string in that format? (Where I learnt the AM/PM system, 12:30 AM is correct for half past midnight, BTW.)
– Ole V.V.
Nov 26 '18 at 12:43
Yes it s a String format. and I d need all the hours between midnight and 1 am to be changed into 00:{the right minute}:00
– Jean
Nov 26 '18 at 13:32