#Incorrect Time Zone Appearing
1 messages ยท Page 1 of 1 (latest)
If the zone-id is invalid, it defaults to GMT. Which of the 2 did you use?
You also need to take into account whether your used elements have zoning information.
See for reference the new datetime classes such as ZonedDateTime.
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
Snippets
Snippet 23, VALID
System.out.println(ZonedDateTime.now());```
## System out
2024-09-04T20:39:36.586061437Z[GMT]
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
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]
then yeah, we want to find or make a DateTImeFormatter
we have the information, we just need to display it
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
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"
Detected code, here are some useful tools:
System out
[Nothing]
@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 ๐
@lunar cypress
The bot will not automatically call methods, so you have to call the method
@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);
Detected code, here are some useful tools:
System out
Sun Sep 01 18:42:32 EDT 2024
Mon Sep 30 18:42:33 EDT 2024
@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
Detected code, here are some useful tools:
System out
Sun Sep 01 18:50:25 EDT 2024
Mon Sep 30 18:50:26 EDT 2024
focus on accomplishing it period
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
That does it a bit different
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
it is close - but thats why we can make our own
yeah
oh no no no
Don't go through the string
For the current month There's YearMonth.now()
@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 ๐
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 ๐
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 ๐