Package PamUtils

Class PamCalendar

java.lang.Object
PamUtils.PamCalendar

public class PamCalendar extends Object
Author:
Doug Gillespie Date and time in Pamguard are critical. It's likely that we'll be taking time from the GPS in the longer term, so for now, make sure that all calendar functions come via this
  • Field Details

  • Constructor Details

    • PamCalendar

      public PamCalendar()
  • Method Details

    • getTimeInMillis

      public static long getTimeInMillis()
      If files are being analysed, return the time based on the file position. Otherwise just take the normal system time.
      Returns:
      time in milliseconds
    • getTime

      public static long getTime()
      a formatted time string
    • getDate

      public static String getDate()
      Returns:
      a formatted date string
    • isSameDay

      public static final boolean isSameDay(long t1, long t2)
      Compares two times in milliseconds to see if they are on the same day or not.
      Parameters:
      t1 - first time
      t2 - second time
      Returns:
      true if times are on the same day
    • getCalendarDate

      public static Calendar getCalendarDate()
      Get the current date
      Returns:
      the date as a Calendar object (in GMT)
    • getCalendarDate

      public static Calendar getCalendarDate(long timeInMillis)
      Get the date for a given time
      Parameters:
      timeInMillis - time in milliseconds
      Returns:
      the date as a Calendar object (in GMT)
    • getDisplayTimeZone

      public static TimeZone getDisplayTimeZone(boolean useLocal)
    • formatDateTime

      public static String formatDateTime(Date date)
    • formatDateTime

      public static String formatDateTime(Date date, boolean useLocal)
      Get a formatted date and time string.
      Parameters:
      date - Date
      Returns:
      formatted String
    • formatDateTime

      public static String formatDateTime(long timeInMillis)
      Formats the time and data in a long format
      Parameters:
      timeInMillis - time in milliseconds
      Returns:
      formated data and time
    • formatDateTime

      public static String formatDateTime(long timeInMillis, boolean useLocal)
      Formats the time and data in a long format
      Parameters:
      timeInMillis - time in milliseconds
      Returns:
      formated data and time
    • formatTodaysTime

      public static String formatTodaysTime(long timeInMillis)
    • formatTodaysTime

      public static String formatTodaysTime(long timeInMillis, boolean useLocal)
    • formatLocalDateTime

      public static String formatLocalDateTime(long timeInMillis)
      Formats the local time and data in a long format
      Parameters:
      timeInMillis - time in milliseconds
      Returns:
      formated data and time
    • formatLocalDateTime

      public static String formatLocalDateTime(Date date)
      Get a formatted local date and time string.
      Parameters:
      date - Date
      Returns:
      formatted String
    • formatLocalDateTime2

      public static String formatLocalDateTime2(long timeInMillis)
      Formats the local time and date in a long format - identical to formatDateTime2(long), but using the local-PC time zone
      Parameters:
      timeInMillis - time in milliseconds
      Returns:
      formated local date and time
    • formatLocalDateTime2

      public static String formatLocalDateTime2(long timeInMillis, String format, boolean useLocal)
      Get a formatted local date and time string - identical to formatDateTime2(long, String), but using the local-PC time zone
      Parameters:
      timeInMillis - time in milliseconds
      the - output format. e.g. "dd MMM yyyy HH:mm:ss"
      Returns:
      formated date and time
    • formatDateTime2

      public static String formatDateTime2(long timeInMillis)
      Formats the time and data in a long format but without the GMT label at the end.
      Parameters:
      timeInMillis - time in milliseconds
      Returns:
      formated data and time
    • formatDateTime2

      public static String formatDateTime2(long timeInMillis, boolean useLocal)
      Formats the time and data in a long format but without the GMT label at the end.
      Parameters:
      timeInMillis - time in milliseconds
      Returns:
      formated data and time
    • formatDateTime2

      public static String formatDateTime2(long timeInMillis, String format, boolean useLocal)
      Formats the time and data in a long format but without the GMT label at the end.
      Parameters:
      timeInMillis - time in milliseconds
      the - output format. e.g. "dd MMM yyyy HH:mm:ss"
      Returns:
      formated data and time
    • millisFromDateTimeString

      public static long millisFromDateTimeString(String dateTime, boolean stringInUTC)
      Return the time in milliseconds from the passed String sing the format "dd MMM yyyy HH:mm:ss"
      Parameters:
      dateTime - The date and time to parse
      stringInUTC - true if the passed String is in UTC time, or false if in local PC time
      Returns:
      epoch time (elapsed time in milliseconds from Jan 1, 1970)
    • millisFromDateTimeString

      public static long millisFromDateTimeString(String dateTime, String format, boolean stringInUTC)
      Return the time in milliseconds from the passed String, using the passed format
      Parameters:
      dateTime - The date and time to parse
      format - The date/time format to use
      stringInUTC - true if the passed String is in UTC time, or false if in local PC time
      Returns:
      epoch time (elapsed time in milliseconds from Jan 1, 1970)
    • formatDBDateTime

      public static String formatDBDateTime(long timeInMillis)
      Formats the date and time in the correct format for database output.

      "yyyy-MM-dd HH:mm:ss"

      Parameters:
      timeInMillis - time in milliseconds
      Returns:
      formated data and time
    • formatDBDateTime

      public static String formatDBDateTime(long timeInMillis, boolean showMillis)
      Formats the date and time in the correct format for database output.

      "yyyy-MM-dd HH:mm:ss"

      Parameters:
      timeInMillis - time in milliseconds
      showMills - also show millseconds.
      Returns:
      formated data and time
    • formatDBStyleTime

      public static String formatDBStyleTime(long timeInMillis, boolean showMillis, boolean useLocal)
    • formatLocalDBDateTime

      public static String formatLocalDBDateTime(long timeInMillis)
      Formats the date and time in the correct format for database output but in local time.

      "yyyy-MM-dd HH:mm:ss"

      Parameters:
      timeInMillis - time in milliseconds
      Returns:
      formated data and time
    • formatDBDate

      public static String formatDBDate(long timeInMillis)
      Formats the date and time in the correct format for database output.

      "yyyy-MM-dd" This will always be UTC since it's data !

      Parameters:
      timeInMillis - time in milliseconds
      Returns:
      formated data and time
    • formatTime

      public static String formatTime(long timeMillis)
      Format a time string in the format HH:MM:SS
      Parameters:
      timeMillis - time in milliseconds
      Returns:
      formatted string
    • formatTime

      public static String formatTime(long timeMillis, boolean showMills)
      Format a time string in the format HH:MM:SS
      Parameters:
      timeMillis - time in milliseconds
      Returns:
      formatted string
    • formatTime

      public static String formatTime(long timeMillis, boolean showMillis, boolean useLocal)
      Format a time string optionally showing the milliseconds with a given precision for UTC time zone
      Parameters:
      timeMillis - time in milliseconds
      millisDigits - number of millisecond decimal places.
      Returns:
      formatted time string.
    • formatLocalTime

      public static String formatLocalTime(long timeMillis, boolean showMillis)
      Format a time string optionally showing the milliseconds with a given precision for UTC time zone
      Parameters:
      timeMillis - time in milliseconds
      millisDigits - number of millisecond decimal places.
      Returns:
      formatted time string.
    • formatTime

      public static String formatTime(long timeMillis, int millisDigits)
      Format a time string optionally showing the milliseconds with a given precision. The time string is formatted as HH:mm:ss.SSSSS.
      Parameters:
      timeMillis - time in milliseconds
      millisDigits - number of millsecond decimal places.
      Returns:
      formatted time string.
    • formatTime

      public static String formatTime(long timeMillis, int millisDigits, boolean useLocal)
      Format a time string optionally showing the milliseconds with a given precision. The time string is formatted as HH:mm:ss.SSSSS.
      Parameters:
      timeMillis - time in milliseconds
      millisDigits - number of millsecond decimal places.
      Returns:
      formatted time string.
    • formatTime2

      public static String formatTime2(long timeMillis, int millisDigits, boolean useLocal)
      Format a time string optionally showing the milliseconds with a given precision. The time string is formatted as HHmmss.SSSSS.
      Parameters:
      timeMillis - time in milliseconds
      millisDigits - number of millsecond decimal places.
      Returns:
      formatted time string.
    • formatDuration

      public static String formatDuration(long timeInMillis)
      Format a time in milliseconds as a number of days / seconds, etc.
      Parameters:
      timeInMillis - time in milliseconds.
      Returns:
      formatted time interval
    • formatDuration

      public static String formatDuration(long timeInMillis, String middleString)
    • formatDuration

      public static String formatDuration(long timeInMillis, String middleString1, String middleString2)
    • formatDate

      public static String formatDate(long timeInMillis)
      Format the data in the dd MMMM yyyy format
      Parameters:
      timeInMillis - time in milliseconds
      Returns:
      formatted string.
    • formatDate

      public static String formatDate(long timeInMillis, boolean useLocal)
      Format the data in the dd MMMM yyyy format
      Parameters:
      timeInMillis - time in milliseconds
      Returns:
      formatted string.
    • formatDate2

      public static String formatDate2(long timeInMillis, boolean useLocal)
      Format the data in the yyMMdd format
      Parameters:
      timeInMillis - time in milliseconds
      Returns:
      formatted string.
    • formatCompactDate

      public static String formatCompactDate(long timeMillis, boolean useLocal)
      Format date in format "yyyyMMdd" using GMT as the time zone
      Parameters:
      timeMillis -
      Returns:
    • formatFileDate

      public static String formatFileDate(long timeMillis)
      Format date in format "yyyyMMdd" but using a time zone based on UTC or a local time depending on global PAMGuard options.
      Parameters:
      timeMillis -
      Returns:
      formatted date string
    • formatFileDate

      public static String formatFileDate(long timeMillis, boolean useLocal)
      Format date in format "yyyyMMdd" but using a time zone based on UTC or a local time depending on global PAMGuard options.
      Parameters:
      timeMillis -
      Returns:
      formatted date string
    • formatFileDateTime

      public static String formatFileDateTime(long timeMillis, boolean useLocal)
      Format date and time in format "yyyyMMdd_HHmmss"
      Parameters:
      timeMillis -
      Returns:
      formatted time string
    • formatLocalFileDateTime

      public static String formatLocalFileDateTime(long timeMillis)
      Format date and time in format "yyyyMMdd_HHmmss", but using the local pc time zone
      Parameters:
      timeMillis -
      Returns:
      formatted time string
    • msFromDateString

      public static long msFromDateString(String dateString)
      Read a date string and turn it into a millisecond time.
      Parameters:
      dateString -
      Returns:
      time in milliseconds
    • msFromDateString

      public static long msFromDateString(String dateString, boolean useLocal)
      Read a date string and turn it into a millisecond time.
      Parameters:
      dateString -
      Returns:
      time in milliseconds
    • millisFromDateString

      public static Long millisFromDateString(String dateString, boolean useLocal)
      Convert a date string into a millisecond time. Return null if the string cannot be interpreted as a date.
      Parameters:
      dateString - Date string
      Returns:
      Millis from 1970 or null.
    • dateFromDateString

      public static Date dateFromDateString(String dateString, boolean useLocal)
      Read a date string and turn it into a Date
      Parameters:
      dateString -
      Returns:
      time as a Date object
    • msFromTimeString

      public static long msFromTimeString(String timeString)
      Read a time string and turn it into a millisecond time.
      Parameters:
      timeString -
      Returns:
      time in milliseconds
    • msFromTimeString

      public static long msFromTimeString(String timeString, boolean useLocal)
      Read a time string and turn it into a millisecond time.
      Parameters:
      timeString -
      useLocal - use local time, not UTC
      Returns:
      time in milliseconds
    • timeFromTimeString

      public static Date timeFromTimeString(String timeString, boolean useLocal)
      Read a time string and turn it into a Date
      Parameters:
      timeString -
      Returns:
      time as a Date object
    • isSoundFile

      public static boolean isSoundFile()
      Returns:
      true if the sound source is a file
    • setSoundFile

      public static void setSoundFile(boolean soundFile)
      Parameters:
      soundFile - set whether the sound source is a file
    • getSessionStartTime

      public static long getSessionStartTime()
      Returns:
      The time that processing started
    • setSessionStartTime

      public static void setSessionStartTime(long sessionStartTime)
      Parameters:
      sessionStartTime - the time that processing started
    • setSoundFileTimeInMillis

      public static void setSoundFileTimeInMillis(long soundFileTimeMillis)
      Relative time within a sound file. This is always just added to sessionStartTime to give an absolute time.
      Parameters:
      soundFileTimeMillis - The relative time of a sound file.
    • createFileName

      public static String createFileName(long fileStartTime, String directory, String prefix, String fileType)
      Create a file name based on a time and other information
      Parameters:
      fileStartTime - File time
      directory - Directory / folder
      prefix - file prefix (part of file name to inlcude before the time stamp)
      fileType - file end
      Returns:
      File path and name, ending with a time stamp
    • createFileNameMillis

      public static String createFileNameMillis(long fileStartTime, String directory, String prefix, String fileType)
      Create a file name based on a time and other information
      Parameters:
      fileStartTime - File time
      directory - Directory / folder
      prefix - file prefix (part of file name to include before the time stamp)
      fileType - file end
      Returns:
      File path and name, ending with a time stamp
    • unpackStandardDateTime

      public static Long unpackStandardDateTime(String numstr)
    • unpackUnknownDateString

      public static Date unpackUnknownDateString(String numstr)
    • createFileName

      public static String createFileName(String directory, String prefix, String fileType)
      Create a file name that doesn't contain a time
      Parameters:
      directory - Directory / folder
      prefix - file prefix (part of file name to inlcude before the time stamp)
      fileType - file end
      Returns:
      File path and name
    • createFileName

      public static String createFileName(long fileStartTime, String prefix, String fileType)
      Creates a file name containing the time and a user defined prefix and file end
      Parameters:
      fileStartTime - time
      prefix - prefix for file name
      fileType - file type (with or without the '.')
      Returns:
      file name
    • createFileNameMillis

      public static String createFileNameMillis(long fileStartTime, String prefix, String fileType)
      Like createFileName but the time now also includes milliseconds.
      Parameters:
      fileStartTime -
      prefix -
      fileType -
      Returns:
    • getUnpunctuatedDate

      public static String getUnpunctuatedDate(boolean useLocal)
      Returns:
      a date string in a very compressed format (suitable for file names)
    • setViewTimes

      public static void setViewTimes(long start, long end)
    • getViewEndTime

      public static long getViewEndTime()
    • getViewPosition

      public static long getViewPosition()
    • setViewPosition

      public static void setViewPosition(long viewPosition)
    • millistoDateNum

      public static double millistoDateNum(long timeMillis)
      Convert millis to MATLAB datenum.
      Parameters:
      timeMillis - - Java datetime millis
      Returns:
      MATLAB datenum.
    • dateNumtoMillis

      public static long dateNumtoMillis(double datenum)
      Convert MATLAB datenum to millis;
      Parameters:
      MATLAB - datenum
      Returns:
      timeMillis equivalent of the MATLAB datenum.
    • millisToUnixEpoch

      public static long millisToUnixEpoch(long millis)
      Convert to Unix Epoch. This is used by R.
      Parameters:
      millis - - Java datetime millis
    • millistoExcelSerial

      public static double millistoExcelSerial(long timeMillis)
      Converts millis to an excel serial data based on the Jan 1900 system.
      Parameters:
      timeMillis -
      Returns:
      excel serial datenum.
    • excelSerialtoMillis

      public static long excelSerialtoMillis(double excelSerial)
      Converts excel Serial date number (Jan 1900 format) to millis.
      Parameters:
      excelSerial - datenum
      Returns:
      timeMillis equivalent of the excel datenum.
    • msFromDate

      public static long msFromDate(int year, int month, int day, int hour, int minute, int second, int millis)
      Convert date values to millisecond time
      Parameters:
      dateString -
      Returns:
      time in milliseconds
    • readTimeDuration

      public static Long readTimeDuration(String duration)
      Read the duration and convert back to millis.
      Parameters:
      duration - - string representation of duration. Can be complete gobbly gook if inputed by user.
      Returns:
      the datenum of the duration in millis.
    • setTimeCorrection

      public static void setTimeCorrection(long correction)
      Set a time correction to add to all calls to gettimemillis();
      Parameters:
      correction - time correction in millis.
    • getTimeCorrection

      public static long getTimeCorrection()
      Get a time correction added to all calls to gettimemillis();
      Returns:
      time correction in millis.