OffsetDateTime toString() return different format date string
I have date in this format yyyy-MM-dd'T'HH:mm:ss'Z'
, but while i'm parsing this using OffsetDateTime.parse(date);
it returns the string by elimination seconds
Logic : Get the day from date, if it is Saturday
or Sunday
change day to monday and return the date String
String date = "2018-12-30T06:00:00Z";
System.out.println(date);
try {
OffsetDateTime dateTime = OffsetDateTime.parse(date);
System.out.println(dateTime); //2018-12-30T06:00Z
DayOfWeek day = dateTime.getDayOfWeek();
// check if price change date is Sunday or Saturday and change it to Monday
if (day.equals(DayOfWeek.SATURDAY) || day.equals(DayOfWeek.SUNDAY)) {
String finalDateTime = dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).toString();
System.out.println(finalDateTime); //2018-12-31T06:00Z
}else {
System.out.println(date);
}
}catch(Exception ex) {
System.out.println(ex);
System.out.println(date);
}
I need to return string as same input format yyyy-MM-dd'T'HH:mm:ss'Z'
java java-date
add a comment |
I have date in this format yyyy-MM-dd'T'HH:mm:ss'Z'
, but while i'm parsing this using OffsetDateTime.parse(date);
it returns the string by elimination seconds
Logic : Get the day from date, if it is Saturday
or Sunday
change day to monday and return the date String
String date = "2018-12-30T06:00:00Z";
System.out.println(date);
try {
OffsetDateTime dateTime = OffsetDateTime.parse(date);
System.out.println(dateTime); //2018-12-30T06:00Z
DayOfWeek day = dateTime.getDayOfWeek();
// check if price change date is Sunday or Saturday and change it to Monday
if (day.equals(DayOfWeek.SATURDAY) || day.equals(DayOfWeek.SUNDAY)) {
String finalDateTime = dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).toString();
System.out.println(finalDateTime); //2018-12-31T06:00Z
}else {
System.out.println(date);
}
}catch(Exception ex) {
System.out.println(ex);
System.out.println(date);
}
I need to return string as same input format yyyy-MM-dd'T'HH:mm:ss'Z'
java java-date
Why don't you use LocalDateTime?
– Nicholas K
Jan 2 at 16:57
honestly i'm not good at java date time, but i have an offset in String rightZ
?
– Deadpool
Jan 2 at 16:58
1
Don't usetoString()
, useDateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").format(dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)))
– ernest_k
Jan 2 at 17:02
That works thank you @ernest_k
– Deadpool
Jan 2 at 17:05
add a comment |
I have date in this format yyyy-MM-dd'T'HH:mm:ss'Z'
, but while i'm parsing this using OffsetDateTime.parse(date);
it returns the string by elimination seconds
Logic : Get the day from date, if it is Saturday
or Sunday
change day to monday and return the date String
String date = "2018-12-30T06:00:00Z";
System.out.println(date);
try {
OffsetDateTime dateTime = OffsetDateTime.parse(date);
System.out.println(dateTime); //2018-12-30T06:00Z
DayOfWeek day = dateTime.getDayOfWeek();
// check if price change date is Sunday or Saturday and change it to Monday
if (day.equals(DayOfWeek.SATURDAY) || day.equals(DayOfWeek.SUNDAY)) {
String finalDateTime = dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).toString();
System.out.println(finalDateTime); //2018-12-31T06:00Z
}else {
System.out.println(date);
}
}catch(Exception ex) {
System.out.println(ex);
System.out.println(date);
}
I need to return string as same input format yyyy-MM-dd'T'HH:mm:ss'Z'
java java-date
I have date in this format yyyy-MM-dd'T'HH:mm:ss'Z'
, but while i'm parsing this using OffsetDateTime.parse(date);
it returns the string by elimination seconds
Logic : Get the day from date, if it is Saturday
or Sunday
change day to monday and return the date String
String date = "2018-12-30T06:00:00Z";
System.out.println(date);
try {
OffsetDateTime dateTime = OffsetDateTime.parse(date);
System.out.println(dateTime); //2018-12-30T06:00Z
DayOfWeek day = dateTime.getDayOfWeek();
// check if price change date is Sunday or Saturday and change it to Monday
if (day.equals(DayOfWeek.SATURDAY) || day.equals(DayOfWeek.SUNDAY)) {
String finalDateTime = dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).toString();
System.out.println(finalDateTime); //2018-12-31T06:00Z
}else {
System.out.println(date);
}
}catch(Exception ex) {
System.out.println(ex);
System.out.println(date);
}
I need to return string as same input format yyyy-MM-dd'T'HH:mm:ss'Z'
java java-date
java java-date
edited Jan 2 at 17:28
Deadpool
asked Jan 2 at 16:55
DeadpoolDeadpool
7,4572829
7,4572829
Why don't you use LocalDateTime?
– Nicholas K
Jan 2 at 16:57
honestly i'm not good at java date time, but i have an offset in String rightZ
?
– Deadpool
Jan 2 at 16:58
1
Don't usetoString()
, useDateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").format(dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)))
– ernest_k
Jan 2 at 17:02
That works thank you @ernest_k
– Deadpool
Jan 2 at 17:05
add a comment |
Why don't you use LocalDateTime?
– Nicholas K
Jan 2 at 16:57
honestly i'm not good at java date time, but i have an offset in String rightZ
?
– Deadpool
Jan 2 at 16:58
1
Don't usetoString()
, useDateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").format(dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)))
– ernest_k
Jan 2 at 17:02
That works thank you @ernest_k
– Deadpool
Jan 2 at 17:05
Why don't you use LocalDateTime?
– Nicholas K
Jan 2 at 16:57
Why don't you use LocalDateTime?
– Nicholas K
Jan 2 at 16:57
honestly i'm not good at java date time, but i have an offset in String right
Z
?– Deadpool
Jan 2 at 16:58
honestly i'm not good at java date time, but i have an offset in String right
Z
?– Deadpool
Jan 2 at 16:58
1
1
Don't use
toString()
, use DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").format(dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)))
– ernest_k
Jan 2 at 17:02
Don't use
toString()
, use DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").format(dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)))
– ernest_k
Jan 2 at 17:02
That works thank you @ernest_k
– Deadpool
Jan 2 at 17:05
That works thank you @ernest_k
– Deadpool
Jan 2 at 17:05
add a comment |
1 Answer
1
active
oldest
votes
As per OffsetDateTime.toString()
method javadoc the shortest possible format for the value is used while omitted parts are implied to be zero. The shortest possible format for 2018-12-30T06:00:00Z
is uuuu-MM-dd'T'HH:mmXXXXX
so the seconds and nanos are skipped:
The output will be one of the following ISO-8601 formats:
- uuuu-MM-dd'T'HH:mmXXXXX
- uuuu-MM-dd'T'HH:mm:ssXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSSSSXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSSXXXXX
The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.
If you need a precise format use a DateTimeFormatter
instance with specific pattern to output the date:
String date = "2018-12-30T06:00:00Z";
OffsetDateTime dt = OffsetDateTime.parse(date);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
System.out.println(fmt.format(dt));
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%2f54010217%2foffsetdatetime-tostring-return-different-format-date-string%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
As per OffsetDateTime.toString()
method javadoc the shortest possible format for the value is used while omitted parts are implied to be zero. The shortest possible format for 2018-12-30T06:00:00Z
is uuuu-MM-dd'T'HH:mmXXXXX
so the seconds and nanos are skipped:
The output will be one of the following ISO-8601 formats:
- uuuu-MM-dd'T'HH:mmXXXXX
- uuuu-MM-dd'T'HH:mm:ssXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSSSSXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSSXXXXX
The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.
If you need a precise format use a DateTimeFormatter
instance with specific pattern to output the date:
String date = "2018-12-30T06:00:00Z";
OffsetDateTime dt = OffsetDateTime.parse(date);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
System.out.println(fmt.format(dt));
add a comment |
As per OffsetDateTime.toString()
method javadoc the shortest possible format for the value is used while omitted parts are implied to be zero. The shortest possible format for 2018-12-30T06:00:00Z
is uuuu-MM-dd'T'HH:mmXXXXX
so the seconds and nanos are skipped:
The output will be one of the following ISO-8601 formats:
- uuuu-MM-dd'T'HH:mmXXXXX
- uuuu-MM-dd'T'HH:mm:ssXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSSSSXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSSXXXXX
The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.
If you need a precise format use a DateTimeFormatter
instance with specific pattern to output the date:
String date = "2018-12-30T06:00:00Z";
OffsetDateTime dt = OffsetDateTime.parse(date);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
System.out.println(fmt.format(dt));
add a comment |
As per OffsetDateTime.toString()
method javadoc the shortest possible format for the value is used while omitted parts are implied to be zero. The shortest possible format for 2018-12-30T06:00:00Z
is uuuu-MM-dd'T'HH:mmXXXXX
so the seconds and nanos are skipped:
The output will be one of the following ISO-8601 formats:
- uuuu-MM-dd'T'HH:mmXXXXX
- uuuu-MM-dd'T'HH:mm:ssXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSSSSXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSSXXXXX
The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.
If you need a precise format use a DateTimeFormatter
instance with specific pattern to output the date:
String date = "2018-12-30T06:00:00Z";
OffsetDateTime dt = OffsetDateTime.parse(date);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
System.out.println(fmt.format(dt));
As per OffsetDateTime.toString()
method javadoc the shortest possible format for the value is used while omitted parts are implied to be zero. The shortest possible format for 2018-12-30T06:00:00Z
is uuuu-MM-dd'T'HH:mmXXXXX
so the seconds and nanos are skipped:
The output will be one of the following ISO-8601 formats:
- uuuu-MM-dd'T'HH:mmXXXXX
- uuuu-MM-dd'T'HH:mm:ssXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSSSSXXXXX
- uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSSXXXXX
The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.
If you need a precise format use a DateTimeFormatter
instance with specific pattern to output the date:
String date = "2018-12-30T06:00:00Z";
OffsetDateTime dt = OffsetDateTime.parse(date);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
System.out.println(fmt.format(dt));
edited Jan 2 at 17:08
answered Jan 2 at 17:02


Karol DowbeckiKarol Dowbecki
25.4k93759
25.4k93759
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%2f54010217%2foffsetdatetime-tostring-return-different-format-date-string%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
Why don't you use LocalDateTime?
– Nicholas K
Jan 2 at 16:57
honestly i'm not good at java date time, but i have an offset in String right
Z
?– Deadpool
Jan 2 at 16:58
1
Don't use
toString()
, useDateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").format(dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)))
– ernest_k
Jan 2 at 17:02
That works thank you @ernest_k
– Deadpool
Jan 2 at 17:05