#Incorrect Time Zone Appearing

1 messages ยท Page 1 of 1 (latest)

vale mountainBOT
#

Detected code, here are some useful tools:

System out

[Nothing]

#

<@&987246399047479336> please have a look, thanks.

shy token
#

If the zone-id is invalid, it defaults to GMT. Which of the 2 did you use?

shy token
#

You also need to take into account whether your used elements have zoning information.

#

See for reference the new datetime classes such as ZonedDateTime.

noble nebula
#

So for that

#

first, start with a ZonedDateTime

#

a Time is like 08:38:13

#

a Date is like Sep 01 ... 2024

#

add to that a time zone and you have enough to say it is Sunday and we know a timezone like EDT

#

so a ZonedDateTime holds all 3 of those pieces of information

vale mountainBOT
#
Ethan McCue's result

Snippets

Snippet 23, VALID

System.out.println(ZonedDateTime.now());```
## System out

2024-09-04T20:39:36.586061437Z[GMT]

noble nebula
#

so if you do System.out.println(ZonedDateTime.now()) you will get a zoned date time for whatever timezone your computer thinks it is in

#

sorry had to go for a sec

#

so yeah now that you have a ZonedDateTime

#

you can choose to change the timezone of it to what you want

#

or you can get a zoned date time for whatever timezone you want

vale mountainBOT
#
Ethan McCue's result

Snippets

Snippet 24, VALID

System.out.println(ZonedDateTime.now(ZoneId.of("America/New_York")));```
## System out

2024-09-04T16:49:25.649251713-04:00[America/New_York]

noble nebula
#

then yeah, we want to find or make a DateTImeFormatter

#

we have the information, we just need to display it

vale mountainBOT
#
Ethan McCue's result

Snippets

Snippet 25, VALID

System.out.println(         ZonedDateTime.now(ZoneId.of("America/New_York"))             .format(DateTimeFormatter.RFC_1123_DATE_TIME)     );```
## System out

Wed, 4 Sep 2024 16:53:52 -0400

noble nebula
#

but yeah - dig into date time formatter or other ways to make a string

#

but fundamentally understanding ZonedDateTime will help you a lot

#

since OffsetDateTime, LocalDateTime, LocalDate, etc. follow the same sorts of logic

#

i.e. "this holds X"

vale mountainBOT
#

Detected code, here are some useful tools:

System out

[Nothing]

hollow tulip
#

@noble nebula oh you are using jshell ๐Ÿ™‚
note that you don't need to write System.out.println, you can just write println... or you don't actually need to print it at the first place ๐Ÿ™‚

hollow tulip
#

@lunar cypress and like Ethan said, you shouldn't use Calendar or Date class, but use java.time.* classes

#
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;

class Main {
    public static void main(String[] args) {
        ZoneId zoneId = ZoneId.of("America/New_York");

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z YYYY");
        
        String dateTime1 = ZonedDateTime.now(zoneId).withDayOfMonth(1).format(formatter);

        int lastDayOfMonth = ZonedDateTime.now().with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();

        String dateTime2 = ZonedDateTime.now(zoneId).withDayOfMonth(lastDayOfMonth).format(formatter);
        
        System.out.println(dateTime1);
        
        System.out.println(dateTime2);
    }
}
Main.main(null);
vale mountainBOT
hollow tulip
#

@lunar cypress see

#

also

#

why do you need the second step ?

#

why can't you do lastDayOfMonth directly on the last step ?

#
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;
ZoneId zoneId = ZoneId.of("America/New_York");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z YYYY");      
String dateTime1 = ZonedDateTime.now(zoneId).withDayOfMonth(1).format(formatter);
String dateTime2 = ZonedDateTime.now(zoneId).with(TemporalAdjusters.lastDayOfMonth()).format(formatter);
System.out.println(dateTime1);        
System.out.println(dateTime2);
#

something like this

vale mountainBOT
hollow tulip
#

@lunar cypress

#

see

noble nebula
#

focus on accomplishing it period

noble nebula
simple wigeon
#

That pattern looks AN AWFUL lot like RFC_1123_DATE_TIME

#

Which is a constant in the DateTimeFormatter class

#

And the format for HTTP headers

noble nebula
#

see above where that is used

#
Wed, 4 Sep 2024 16:53:52 -0400
#

the pattern gives

#
Sun Sep 01 18:50:25 EDT 2024
simple wigeon
#

ah I see

#

my b

noble nebula
#

it is close - but thats why we can make our own

simple wigeon
#

yeah

simple wigeon
#

oh no no no

#

Don't go through the string

#

For the current month There's YearMonth.now()

vale mountainBOT
#

@lunar cypress

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure ๐Ÿ‘

vale mountainBOT
#

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure ๐Ÿ‘

vale mountainBOT
#

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure ๐Ÿ‘