Package com.stata.sfi

Class DateTime

java.lang.Object
com.stata.sfi.DateTime

public class DateTime extends Object
This class provides a set of core tools for interacting with Stata's date and time values. Translations can be done from Stata internal form (SIF) to LocalDateTime and vice versa.

Examples:

These examples show how to use SIF values from Stata with instances of LocalDateTime. We will use Stata's Java integration (that is, java:) to illustrate.


java:
    import java.time.LocalDateTime;
    public class Examples {
        public static void sifClockToLocalDateTime(double sif) throws SIFDateTimeException  {
            // convert the sif clock to LocalDateTime
            LocalDateTime ldt = DateTime.getLocalDateTime(sif, "%tc");
            
            // print them both
            String prettyFormat = "%tcMonth_dd,_CCYY_HH:MM:SS.sss";
            SFIToolkit.displayln(SFIToolkit.formatValue(sif, prettyFormat).trim());
            SFIToolkit.displayln(ldt.toString());
        }
        
        public static void sifDateToLocalDateTime(double sif) throws SIFDateTimeException {
            // convert the sif date to LocalDateTime
            LocalDateTime ldt = DateTime.getLocalDateTime(sif, "%td");
            
            // print them both
            SFIToolkit.displayln(SFIToolkit.formatValue(sif, "%td").trim());
            SFIToolkit.displayln(ldt.toLocalDate().toString());
        }
        
        public static void localDateTimeToSIF(LocalDateTime ldt) throws SIFDateTimeException {
        	double sif = DateTime.getSIF(ldt, "%tc");
            
            // print them both
            String prettyFormat = "%tcMonth_dd,_CCYY_HH:MM:SS.sss";
            SFIToolkit.displayln(SFIToolkit.formatValue(sif, prettyFormat).trim());
            SFIToolkit.displayln(ldt.toString());
        }
    }
 end
 
.  tempname dt

.  scalar `dt' = clock("January 1, 2030 12:01:01.100", "MDY hms")
.  java: Examples.sifClockToLocalDateTime(Scalar.getValue("`dt'"));
January 1, 2030 12:01:01.100
2030-01-01T12:01:01.100

.  scalar `dt' = date("January 1, 2031", "MDY")
.  java: Examples.sifDateToLocalDateTime(Scalar.getValue("`dt'"));
01jan2031
2031-01-01

.  local nano = 100 * 1000 * 1000
.  java: Examples.localDateTimeToSIF(LocalDateTime.of(2031,1,1,13,1,1,`nano'));
January 1, 2031 13:01:01.100
2031-01-01T13:01:01.10
 
  • Method Details

    • getLocalDateTime

      public static LocalDateTime getLocalDateTime(double value, String format) throws SIFDateTimeException
      Translate a SIF value to a LocalDateTime. SIF dates with milliseconds, will be translated to LocalDateTime nanoseconds. If the SIF has fractional milliseconds, they will be truncated.
      Parameters:
      value - The value in Stata internal form (SIF).
      format - The Stata format (%tc, %tC, %td, %tw, %tm, %tq, %th, %ty).
      Returns:
      The translation to LocalDateTime.
      Throws:
      SIFDateTimeException - If an error occurs.
    • getLocalDateTime

      public static LocalDateTime getLocalDateTime(double value, String format, boolean roundMicroSeconds) throws SIFDateTimeException
      Translate a SIF value to a LocalDateTime. SIF dates with milliseconds, will be translated to LocalDateTime nanoseconds. If the SIF has fractional milliseconds they will be either truncated or rounded to the nearest microsecond depending on the value of roundMicroSeconds.
      Parameters:
      value - The value in Stata internal form (SIF).
      format - The Stata format (%tc, %tC, %td, %tw, %tm, %tq, %th, %ty).
      roundMicroSeconds - When false, fractional milliseconds will be truncated. When true, fractional milliseconds will be rounded to the nearest microsecond. This parameter has an effect with %tc and %tC formats only.
      Returns:
      The translation to LocalDateTime.
      Throws:
      SIFDateTimeException - If an error occurs.
    • getSIF

      public static double getSIF(LocalDateTime dt, String format) throws SIFDateTimeException
      Translate a LocalDateTime to a value in SIF.
      Parameters:
      dt - The LocalDateTime to translate.
      format - The Stata format (%tc, %tC, %td, %tw, %tm, %tq, %th, %ty).
      Returns:
      The value of the SIF.
      Throws:
      SIFDateTimeException - If an error occurs.
    • getSIF

      public static double getSIF(LocalDateTime dt, String format, boolean roundMicroSeconds) throws SIFDateTimeException
      Translate a LocalDateTime to a value in SIF. If the LocalDateTime has nanoseconds, they will be either truncated to milliseconds or rounded to the nearest fractional millisecond depending on the value of roundMicroSeconds.
      Parameters:
      dt - The LocalDateTime to translate.
      format - The Stata format (%tc, %tC, %td, %tw, %tm, %tq, %th, %ty).
      roundMicroSeconds - When false, nanoseconds will be truncated to milliseconds. When true, nanoseconds will be rounded to the nearest fractional millisecond. This parameter has effect with %tc and %tC formats only.
      Returns:
      The value of the SIF.
      Throws:
      SIFDateTimeException - If an error occurs.