-1

I am trying to do it without methods so that I can better grasp the concept.

I am really close. My hours math seems to be off. What am I not understanding there?

static void showCurrent(){
    Date today = new Date();
    long milliseconds = today.getTime(); // ex: 1651773923837
    
    long seconds = milliseconds / 1000;
    long minutes = seconds / 60;
    long hours = minutes / 60;

    long s = seconds % 60;
    long m = minutes % 60;
    long h = hours % 24;

    System.out.printf("Date: %s, Time: %d\n", today.toString(), milliseconds);
    System.out.println(h + ": " + m + ": " + s );

Output:

Date: Fri May 06 10:13:21 EDT 2022, Time: 1651846401839
14: 13: 21
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
xtina_lt
  • 29
  • 6
  • 3
    FYI `Date` is old API. Since Java... 8, I belive, there is new API for dates. Check `LocalDate` and `LocalDateTime` – Fenio May 06 '22 at 14:38
  • 1
    You said *without methods*, but you are calling the constructor (I consider that a method too) and the `getTime` method of the `Date` class, a class that was poorly designed and is long outdated. If I understand correctly that you want to do your own math based on milliseconds from the epoch, get those millis either from `Instant.now().toEpochMilli()` or from `System.currentTimeMillis()`. And enjoy the better naming in both cases. – Ole V.V. May 06 '22 at 14:38
  • Yes! I wanted to do the math based on the milliseconds. Figured it would help me understand the concept better as well as modulo. I'm really glad to hear about LocalDate and Instant. That's what I get for practicing with an old book. Going to have fun looking into both of these concepts. You are correct about the better naming as well! – xtina_lt May 09 '22 at 16:24

3 Answers3

2

According to Javadoc about Date::getTime:

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object

The important part is "GMT" which is different from your timezone : EDT, which is... GMT - 4:00

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • This is the correct answer to the question as asked. – Ole V.V. May 06 '22 at 15:11
  • 1
    One may add that if the OP wants to adjust their code for time zone, they need to know when that time zone goes in and out of summer time (daylight saving time). Doesn’t sound trivial to me if they require doing it *without methods*. – Ole V.V. May 06 '22 at 15:13
  • Thanks for the clarity. I thought I was loosing it! – xtina_lt May 09 '22 at 16:16
2

Avoid legacy date-time classes

The toString method on java.util.Date has the unfortunate anti-feature of applying the JVM`s current default time zone while generating the text.

Never use Date. Replaced years ago by the modern java.time classes.

Instant

Use java.time.Instant.

Capture the current moment.

Instant instant = Instant.now() ;

Generate text in standard ISO 8601 format.

String output = instant.toString() ;

Get a count of milliseconds since the epoch reference of first moment of 1970 as seen with an offset from UTC of zero hours-minutes-seconds, 1970-01-01T00:00Z.

long millis = instant.toEpochMilli() ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thanks for the update! I was using one of my old books to get some practice. That's what I get for using an old book. I'm definately going to look at the docs for java.time. Should be fun! – xtina_lt May 09 '22 at 16:18
1

The LocalDateTime.now() method returns the instance of LocalDateTime class so if you print the instance of LocalDateTime class, it will print the current time and time. To get it the the right format you need to format the current date using DateTimeFormatter class included in JDK 1.8

import java.time.format.DateTimeFormatter;  
import java.time.LocalDateTime;    
public class CurrentDateTime {    
  public static void main(String[] args) {    
   DateTimeFormatter date_wanted = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");  
   LocalDateTime now = LocalDateTime.now();  
   System.out.println(date_wanted.format(now));  
  }    
} 
FRS
  • 47
  • 4
  • 1
    For production code and for virtually everyone this is a fine suggestion. Only I doubt that it lives up to what the OP meant by *without methods*. – Ole V.V. May 06 '22 at 15:09