Logo

Programming-Idioms

  • Cobol
  • Lisp
  • Java

Idiom #61 Get current date

Assign to the variable d the current date/time value, in the most standard type.

import static java.util.Calendar.getInstance;
import java.util.Date;
Date a = getInstance().getTime();
String d = "%tc".formatted(a);
import java.time.Instant;
Instant d = Instant.now();

New in Java 8.
Use Date on java 7.
import static java.lang.System.currentTimeMillis;
long a = currentTimeMillis();
String d = "%tc".formatted(a);
IDENTIFICATION DIVISION.
PROGRAM-ID. date.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 curr-date.
   03 year      pic 9(4).
   03 month     pic 9(2).
   03 day       pic 9(2).
PROCEDURE DIVISION.
   MOVE FUNCTION CURRENT-DATE TO curr-date
STOP RUN.
(defparameter d
  (multiple-value-bind (seconds minutes hours day month year day-of-the-week daylight-savings-time-p time-zone)
(get-decoded-time)
(declare (ignorable day-of-the-week daylight-savings-time-p time-zone))
(format nil "~D-~2,'0D-~2,'0D ~2,'0D:~2,'0D:~2,'0D~%" year month day hours minutes seconds))
"The current date and time as a string value.")

GET-DECODED-TIME returns 9 values. We capture them all in MULTIPLE-VALUE-BIND. I chose to ignore day-of-the-week , daylight-savings-time-p , and time-zone , but they are still bound.
Note that Lisp's FORMAT specifier string is very different from most other languages!
with Ada.Calendar;
D : Ada.Calendar.Time := Ada.Calendar.Clock;

New implementation...
< >
programming-idioms.org