Something went wrong. Try again.
Work fork of an Android Jabber client for bridge users git.sr.ht/~singpolyma/cheogram-android
sopranica sgx xmpp jabber
Something went wrong. Try again.
1.6 kB · 44 lines
Java
at upstream
123456789101112131415161718192021222324252627282930313233343536373839404142434445package im.conversations.android.xmpp;
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;
public final class Timestamps {
private Timestamps() { throw new IllegalStateException("Do not instantiate me"); }
public static long parse(final String input) throws ParseException { if (input == null) { throw new IllegalArgumentException("timestamp should not be null"); } final String timestamp = input.replace("Z", "+0000"); final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US); final long milliseconds = getMilliseconds(timestamp); final String formatted = timestamp.substring(0, 19) + timestamp.substring(timestamp.length() - 5); final Date date = simpleDateFormat.parse(formatted); if (date == null) { throw new IllegalArgumentException("Date was null"); } return date.getTime() + milliseconds; }
private static long getMilliseconds(final String timestamp) { if (timestamp.length() >= 25 && timestamp.charAt(19) == '.') { final String millis = timestamp.substring(19, timestamp.length() - 5); try { double fractions = Double.parseDouble("0" + millis); return Math.round(1000 * fractions); } catch (final NumberFormatException e) { return 0; } } else { return 0; } }}