How do I get the current time in .beats (Swatch Internet Time) in Java?
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.
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;
}
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With