All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.easimart.EasimartDateFormat Maven / Gradle / Ivy

package co.easimart;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.SimpleTimeZone;

/**
 * This is the currently used date format. It is precise to the millisecond.
 */
/* package */ class EasimartDateFormat {
  private static final String TAG = "EasimartDateFormat";

  private static final EasimartDateFormat INSTANCE = new EasimartDateFormat();
  public static EasimartDateFormat getInstance() {
    return INSTANCE;
  }

  // SimpleDateFormat isn't inherently thread-safe
  private final Object lock = new Object();

  private final DateFormat dateFormat;

  private EasimartDateFormat() {
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
    format.setTimeZone(new SimpleTimeZone(0, "GMT"));
    dateFormat = format;
  }

  /* package */ Date parse(String dateString) {
    synchronized (lock) {
      try {
        return dateFormat.parse(dateString);
      } catch (java.text.ParseException e) {
        // Should never happen
        EasimartLog.e(TAG, "could not parse date: " + dateString, e);
        return null;
      }
    }
  }

  /* package */ String format(Date date) {
    synchronized (lock) {
      return dateFormat.format(date);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy