Package com.stata.sfi
Class DateTime
java.lang.Object
com.stata.sfi.DateTime
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 Summary
Modifier and TypeMethodDescriptionstatic LocalDateTime
getLocalDateTime
(double value, String format) Translate a SIF value to aLocalDateTime
.static LocalDateTime
getLocalDateTime
(double value, String format, boolean roundMicroSeconds) Translate a SIF value to aLocalDateTime
.static double
getSIF
(LocalDateTime dt, String format) Translate aLocalDateTime
to a value in SIF.static double
getSIF
(LocalDateTime dt, String format, boolean roundMicroSeconds) Translate aLocalDateTime
to a value in SIF.
-
Method Details
-
getLocalDateTime
public static LocalDateTime getLocalDateTime(double value, String format) throws SIFDateTimeException Translate a SIF value to aLocalDateTime
. SIF dates with milliseconds, will be translated toLocalDateTime
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 aLocalDateTime
. SIF dates with milliseconds, will be translated toLocalDateTime
nanoseconds. If the SIF has fractional milliseconds they will be either truncated or rounded to the nearest microsecond depending on the value ofroundMicroSeconds
.- Parameters:
value
- The value in Stata internal form (SIF).format
- The Stata format (%tc, %tC, %td, %tw, %tm, %tq, %th, %ty).roundMicroSeconds
- Whenfalse
, fractional milliseconds will be truncated. Whentrue
, 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
Translate aLocalDateTime
to a value in SIF.- Parameters:
dt
- TheLocalDateTime
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 aLocalDateTime
to a value in SIF. If theLocalDateTime
has nanoseconds, they will be either truncated to milliseconds or rounded to the nearest fractional millisecond depending on the value ofroundMicroSeconds
.- Parameters:
dt
- TheLocalDateTime
to translate.format
- The Stata format (%tc, %tC, %td, %tw, %tm, %tq, %th, %ty).roundMicroSeconds
- Whenfalse
, nanoseconds will be truncated to milliseconds. Whentrue
, 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.
-