1

I have a timestamp in seconds, e.g. 1543845960. I need to retrieve a season from this timestamp. How can I do it?

ZoneId zone = ZoneId.systemDefault();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(zone);
String dt = df.format(Instant.ofEpochMilli(timestamp*1000));

Is there any built-in way to get a season by Zone? E.g. winter, spring, summer or autumn.

ScalaBoy
  • 3,254
  • 13
  • 46
  • 84
  • 6
    No, because there is no default definition of a season. [From Wikipedia](https://en.wikipedia.org/wiki/Winter): "Different cultures define different dates as the start of winter, and some use a definition based on weather.". – Andy Turner Dec 03 '18 at 14:42
  • See @AndyTurner answer. However solstices and equinoxes are standard. – m0skit0 Dec 03 '18 at 14:43
  • 1
    Have a look at this: https://stackoverflow.com/questions/189468/identifying-the-season-from-the-date-using-java – Robert Kock Dec 03 '18 at 14:43
  • 1
    In Queensland, it's hot all year round so they have the wet season and the dry season. – Peter Lawrey Dec 03 '18 at 14:43
  • 1
    My first thought is that zone isn't enough information. You need northern/southern hemisphere as well. – ahillman3 Dec 03 '18 at 14:44
  • @m0skit0 some celebrate winter solstice on Dec 25th ;) – Peter Lawrey Dec 03 '18 at 14:46
  • Might be simpler to convert to day-of-year rather than month and day-of-month. Also see http://farside.ph.utexas.edu/Books/Syntaxis/Almagest/node36.html – John Hascall Dec 03 '18 at 14:48
  • No, there are no seasons built in. You will need to define your own seasons from the needs and understanding of your particular users. BTW, just use `Instant.ofEpochSecond(timeStamp).atZone(zone)` and avoid the multiplication. – Ole V.V. Dec 03 '18 at 14:49
  • @PeterLawrey That's Christmas, not winter solstice :) I mean astronomical solstice and equinox ofc. – m0skit0 Dec 03 '18 at 14:58
  • 1
    Still summer solstice in some places coincides with winter solstice in others. @m0skit0 – Ole V.V. Dec 03 '18 at 15:07
  • @OleV.V. You're correct sir. However your comment-answer does not solve that problem either. – m0skit0 Dec 03 '18 at 15:08
  • @RobertKock: Thanks for the link. I finally used it as an answer. – ScalaBoy Dec 03 '18 at 15:12
  • @RobertKock That answer is also wrong, but I guess it depends on how precise you want to be. – m0skit0 Dec 03 '18 at 15:22
  • @m0skit0 Celebrating on Dec 25th is pre-Christian. https://en.wikipedia.org/wiki/Winter_solstice#History_and_cultural_significance – Peter Lawrey Dec 03 '18 at 16:14
  • 1
    @PeterLawrey I know. I doubt people are still celebrating pre-Christian winter solstice nowdays. And we're digressing here. – m0skit0 Dec 03 '18 at 16:46
  • If you like to see a solution for astronomical seasons then you might consider [my answer](https://stackoverflow.com/a/53662468/2491410) in the linked post. – Meno Hochschild Dec 07 '18 at 02:39

1 Answers1

2

Not possible. For one, seasons are a more or less cultural thing. So the timing is not necessarily dependent on time zones or rather when a season "begins" can vary.

Also, time zones in general dont have much to do with seasons. Time zones are based on longitudal borders and seasons are based on the tilt of the axis the earth rotates around itself. Meaning, that the seasons in the northern and southern hemisphere are countercyclical. So when its summer in the northern hemisphere its winter in the southern hemisphere.

So the problem is not one you can implement generically, depending on time zones.

Benjamin Basmaci
  • 2,247
  • 2
  • 25
  • 46