Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current time in Swatch Internet Time in Java? [closed]

Tags:

java

time

How do I get the current time in .beats (Swatch Internet Time) in Java?

like image 836
stommestack Avatar asked Oct 17 '25 01:10

stommestack


1 Answers

Basically, the formula to calculate the current time in .beats is:

beats = ( UTC+1seconds + ( UTC+1minutes * 60 ) + ( UTC+1hours * 3600 ) ) / 86,4

Round it down and you have the current time in .beats.

New Java Code (java.time)

In Java 8 and later, we have the new java.time package (defined by JSR 310, inspired by Joda-Time, Tutorial, extended by ThreeTen Extra project).

public static int getCurrentTimeInBeats() {
    ZonedDateTime now = ZonedDateTime.now( ZoneId.of( "UTC+01:00" ) );  // "Biel Meantime" = UTC+01:00
    int beats = (int) ( ( now.get( ChronoField.SECOND_OF_MINUTE) + ( now.get( ChronoField.MINUTE_OF_HOUR ) * 60 ) + ( now.get( ChronoField.HOUR_OF_DAY) * 3600 ) ) / 86.4 );
    return beats;
}

Old Java Code

If you cannot use the new java.time package in Java 8 and later, you can use the date-time classes first bundled with earlier versions of Java.

public static int getCurrentTimeInBeats() {
    java.util.Calendar cal = java.util.Calendar.getInstance( java.util.TimeZone.getTimeZone( "GMT+01:00" ) );
    int beats = (int) ( ( cal.get( java.util.Calendar.SECOND ) + ( cal.get( java.util.Calendar.MINUTE ) * 60 ) + ( cal.get( java.util.Calendar.HOUR_OF_DAY ) * 3600 ) ) / 86.4 );
    return beats;
}

Note that the use of "GMT" in GMT+01:00 rather than "UTC", per the doc for TimeZone.getTimeZone.

like image 66
stommestack Avatar answered Oct 18 '25 13:10

stommestack