View Category
Parse a date and time from a string
Given the string
"2008-05-06 13:29", parse it as a date representing 6th March, 2008 1:29:00pm in the local time zone.
perl
#! /usr/bin/perl
# -*- Mode: CPerl -*-
use strict;
use POSIX;
# Given the string "2008-05-06 13:29", parse it as a date
# representing 6th March, 2008 1:29:00pm in the local time zone.
my $ds = "2008-05-06 13:29";
my $y;
my $m;
my $d;
my $hr;
my $mn;
print "Original: ",$ds,"\n";
if ( $ds =~ /(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2})/ ){
$y = $1 - 1900;
$m = $2;
$d = $3;
$hr = $4;
$mn = $5;
printf "Nominal: %s\n",
strftime("%e %B, %Y %l:%M:%S%P",0, $mn , $hr, $d, $m,$y);
my $eth = "";
if ( $d == 1 ){
$eth = "st";
} elsif ( $d == 2 ){
$eth = "nd";
} elsif ( $d == 3 ){
$eth = "rd";
} else {
$eth = "th";
}
printf "As required: %d%s %s\n",$d,$eth,
strftime("%B, %Y %l:%M:%S%P",0, $mn , $hr, $d, $m,$y);
}
#eos
# -*- Mode: CPerl -*-
use strict;
use POSIX;
# Given the string "2008-05-06 13:29", parse it as a date
# representing 6th March, 2008 1:29:00pm in the local time zone.
my $ds = "2008-05-06 13:29";
my $y;
my $m;
my $d;
my $hr;
my $mn;
print "Original: ",$ds,"\n";
if ( $ds =~ /(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2})/ ){
$y = $1 - 1900;
$m = $2;
$d = $3;
$hr = $4;
$mn = $5;
printf "Nominal: %s\n",
strftime("%e %B, %Y %l:%M:%S%P",0, $mn , $hr, $d, $m,$y);
my $eth = "";
if ( $d == 1 ){
$eth = "st";
} elsif ( $d == 2 ){
$eth = "nd";
} elsif ( $d == 3 ){
$eth = "rd";
} else {
$eth = "th";
}
printf "As required: %d%s %s\n",$d,$eth,
strftime("%B, %Y %l:%M:%S%P",0, $mn , $hr, $d, $m,$y);
}
#eos
# Shurely you mean 6th MAY? If not, oh well
use Time::Piece;
my $dt_str = '2008-05-06 13:29';
my $tp = Time::Piece->strptime( $dt_str, '%Y-%m-%d %H:%M');
print $tp,"\n";
use Time::Piece;
my $dt_str = '2008-05-06 13:29';
my $tp = Time::Piece->strptime( $dt_str, '%Y-%m-%d %H:%M');
print $tp,"\n";
java
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = df.parse("2008-05-06 13:29");
Date date = df.parse("2008-05-06 13:29");
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
DateTime dt = fmt.parseDateTime("2008-05-06 13:29");
DateTime dt = fmt.parseDateTime("2008-05-06 13:29");
clojure
(.. (SimpleDateFormat. "yyyy-MM-dd HH:mm")
(parse "2008-05-06 13:29"))
(parse "2008-05-06 13:29"))
fantom
dt := DateTime.fromLocale("2008-05-06 13:29", "YYYY-MM-DD hh:mm")
Display information about a date
Display the day of month, day of year, month name and day name of the day 8 days from now.
perl
#! /usr/bin/perl
# -*- Mode: CPerl -*-
use strict;
use Date::Calc qw(:all);
my $days_in_future = $ARGV[0];
$days_in_future = 8 unless $days_in_future;
my ($year,$month,$day, $hour,$min,$sec, $doy,$dow,$dst) = Localtime();
my ($fyear,$fmonth,$fday) = Add_Delta_Days($year,$month,$day,$days_in_future);
printf "Now: %d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d\n",
$year,$month,$day,$hour,$min,$sec;
printf "Then: %d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d\n",
$fyear,$fmonth,$fday,$hour,$min,$sec;
printf "Then: day of month: %d\n",$fday;
printf "Then: day of year: %d\n",Day_of_Year($fyear,$fmonth,$fday);
printf "Then: day of name: %s\n",
Day_of_Week_to_Text(Day_of_Week($fyear,$fmonth,$fday));
printf "Then: month name: %s\n",Month_to_Text($fmonth);
#eos
# -*- Mode: CPerl -*-
use strict;
use Date::Calc qw(:all);
my $days_in_future = $ARGV[0];
$days_in_future = 8 unless $days_in_future;
my ($year,$month,$day, $hour,$min,$sec, $doy,$dow,$dst) = Localtime();
my ($fyear,$fmonth,$fday) = Add_Delta_Days($year,$month,$day,$days_in_future);
printf "Now: %d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d\n",
$year,$month,$day,$hour,$min,$sec;
printf "Then: %d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d\n",
$fyear,$fmonth,$fday,$hour,$min,$sec;
printf "Then: day of month: %d\n",$fday;
printf "Then: day of year: %d\n",Day_of_Year($fyear,$fmonth,$fday);
printf "Then: day of name: %s\n",
Day_of_Week_to_Text(Day_of_Week($fyear,$fmonth,$fday));
printf "Then: month name: %s\n",Month_to_Text($fmonth);
#eos
use Time::Piece;
use Time::Seconds;
my $t = localtime;
my $t_8 = $t + (ONE_DAY * 8);
printf "Now: %d, %d, %s, %s\n",
$t->day_of_month, $t->day_of_year, $t->fullmonth, $t->fullday;
printf "Then: %d, %d, %s, %s\n",
$t_8->day_of_month, $t_8->day_of_year, $t_8->fullmonth, $t_8->fullday;
use Time::Seconds;
my $t = localtime;
my $t_8 = $t + (ONE_DAY * 8);
printf "Now: %d, %d, %s, %s\n",
$t->day_of_month, $t->day_of_year, $t->fullmonth, $t->fullday;
printf "Then: %d, %d, %s, %s\n",
$t_8->day_of_month, $t_8->day_of_year, $t_8->fullmonth, $t_8->fullday;
java
Calendar cal = Calendar.getInstance();
cal.add(DAY_OF_YEAR, 8);
System.out.println(cal.get(DAY_OF_MONTH));
System.out.println(cal.get(DAY_OF_YEAR));
System.out.println(new SimpleDateFormat("MMMM").format(cal.getTime()));
System.out.println(new SimpleDateFormat("EEEE").format(cal.getTime()));
cal.add(DAY_OF_YEAR, 8);
System.out.println(cal.get(DAY_OF_MONTH));
System.out.println(cal.get(DAY_OF_YEAR));
System.out.println(new SimpleDateFormat("MMMM").format(cal.getTime()));
System.out.println(new SimpleDateFormat("EEEE").format(cal.getTime()));
clojure
(let [cal (Calendar/getInstance)]
(.add cal Calendar/DAY_OF_YEAR 8)
(println (.format (SimpleDateFormat. "d, D, MMMM, EEEE")
(.getTime cal))))
(.add cal Calendar/DAY_OF_YEAR 8)
(println (.format (SimpleDateFormat. "d, D, MMMM, EEEE")
(.getTime cal))))
fantom
date := Date.today + 8day
echo(date.day)
echo(date.dayOfYear)
echo(date.month.localeFull)
echo(date.weekday.localeFull)
echo(date.day)
echo(date.dayOfYear)
echo(date.month.localeFull)
echo(date.weekday.localeFull)
Display a date in different locales
Display a language/locale friendly version of New Year's Day for 2009 for several languages/locales. E.g. for languages English, French, German, Italian, Dutch the output might be something like:
Thursday, January 1, 2009
jeudi 1 janvier 2009
giovedì 1 gennaio 2009
Donnerstag, 1. Januar 2009
donderdag 1 januari 2009
(Indicate in comments where possible if any language specific or operating system configuration needs to be in place.)
Thursday, January 1, 2009
jeudi 1 janvier 2009
giovedì 1 gennaio 2009
Donnerstag, 1. Januar 2009
donderdag 1 januari 2009
(Indicate in comments where possible if any language specific or operating system configuration needs to be in place.)
perl
#!/usr/bin/perl
use warnings;
use strict;
use locale;
use POSIX qw(strftime);
use Time::Local;
my $date=timegm(0,0,0, 1,0,101); #00:00:00 01/01/2001
my $str_time = strftime "%c", gmtime;
print "Date: $str_time\n";
use warnings;
use strict;
use locale;
use POSIX qw(strftime);
use Time::Local;
my $date=timegm(0,0,0, 1,0,101); #00:00:00 01/01/2001
my $str_time = strftime "%c", gmtime;
print "Date: $str_time\n";
java
Calendar cal = Calendar.getInstance();
cal.set(2009, Calendar.JANUARY, 1);
Locale[] locales = { ENGLISH, FRENCH, ITALIAN, GERMAN, new Locale("nl") };
for (Locale l : locales) {
System.out.println(getDateInstance(FULL, l).format(cal.getTime()));
}
cal.set(2009, Calendar.JANUARY, 1);
Locale[] locales = { ENGLISH, FRENCH, ITALIAN, GERMAN, new Locale("nl") };
for (Locale l : locales) {
System.out.println(getDateInstance(FULL, l).format(cal.getTime()));
}
clojure
(let [time (.getTime (GregorianCalendar. 2009 Calendar/JANUARY 1))]
(doseq [locale ["en" "fr" "it" "de" "nl"]]
(println (.format (DateFormat/getDateInstance DateFormat/FULL
(Locale. locale))
time))))
(doseq [locale ["en" "fr" "it" "de" "nl"]]
(println (.format (DateFormat/getDateInstance DateFormat/FULL
(Locale. locale))
time))))
fantom
// May require modification of Fantom distribution t
// for undefined locales - basically just create a '<locale-name>.props' plain text file with values like this:
// sunAbbr=Sun
// ..
// sunFull=Sunday
["en", "fr", "ru"].map { Locale(it) }.each |Locale l| {
l.use { echo(Date(2009, Month.jan, 1).toLocale("WWWW, MMMM D, YYYY")) }
}
// for undefined locales - basically just create a '<locale-name>.props' plain text file with values like this:
// sunAbbr=Sun
// ..
// sunFull=Sunday
["en", "fr", "ru"].map { Locale(it) }.each |Locale l| {
l.use { echo(Date(2009, Month.jan, 1).toLocale("WWWW, MMMM D, YYYY")) }
}
Display the current date and time
Create a Date object representing the current date and time. Print it out.
If you can also do this without creating a Date object you can show that too.
If you can also do this without creating a Date object you can show that too.
perl
use Class::Date;
my $date = Class::Date->now();
print $date->string()."\n";
print localtime()."\n";
my $date = Class::Date->now();
print $date->string()."\n";
print localtime()."\n";
use Time::Piece ();
# Date object
my $date = Time::Piece::localtime;
print "$date\n";
# no object
print scalar(localtime),"\n";
# Date object
my $date = Time::Piece::localtime;
print "$date\n";
# no object
print scalar(localtime),"\n";
java
import java.util.Date;
public class SolutionXX {
public static void main(String[] args) {
Date now = new Date();
System.out.println(now.toString());
}
}
public class SolutionXX {
public static void main(String[] args) {
Date now = new Date();
System.out.println(now.toString());
}
}
clojure
(import 'java.util.Date)
(println (str (Date.)))
(println (str (Date.)))
fantom
echo(DateTime.now)
