Date format: Difference between revisions
(→{{header|Raven}}: same as Perl) |
|||
Line 118: | Line 118: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
import java.util.Calendar; |
|||
import java.util.GregorianCalendar; |
import java.util.GregorianCalendar; |
||
import java.text.DateFormatSymbols; |
|||
public class Dates{ |
public class Dates{ |
||
public static void main(String[] args){ |
public static void main(String[] args){ |
||
Calendar now = new GregorianCalendar(); //months are 0 indexed, dates are 1 indexed |
|||
DateFormatSymbols symbols = new DateFormatSymbols(); //names for our months and weekdays |
|||
//plain numbers way |
//plain numbers way |
||
System.out.println(now.get( |
System.out.println(now.get(Calendar.YEAR) + "-" + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)); |
||
//words way |
//words way |
||
System.out.print(symbols.getWeekdays()[now.get(Calendar.DAY_OF_WEEK)] + ", "); |
|||
System.out.print(symbols.getMonths()[now.get(Calendar.MONTH)] + " "); |
|||
⚫ | |||
case(GregorianCalendar.MONDAY): System.out.print("Monday, ");break; |
|||
case(GregorianCalendar.TUESDAY): System.out.print("Tuesday, ");break; |
|||
case(GregorianCalendar.WEDNESDAY): System.out.print("Wednesday, ");break; |
|||
case(GregorianCalendar.THURSDAY): System.out.print("Thursday, ");break; |
|||
case(GregorianCalendar.FRIDAY): System.out.print("Friday, ");break; |
|||
default: System.out.print("Saturday, ");break; |
|||
} |
|||
switch(now.get(GregorianCalendar.MONTH)){ |
|||
case(GregorianCalendar.JANUARY): System.out.print("January ");break; |
|||
case(GregorianCalendar.FEBRUARY): System.out.print("February ");break; |
|||
case(GregorianCalendar.MARCH): System.out.print("March ");break; |
|||
case(GregorianCalendar.APRIL): System.out.print("April ");break; |
|||
case(GregorianCalendar.MAY): System.out.print("May ");break; |
|||
case(GregorianCalendar.JUNE): System.out.print("June ");break; |
|||
case(GregorianCalendar.JULY): System.out.print("July ");break; |
|||
case(GregorianCalendar.AUGUST): System.out.print("August ");break; |
|||
case(GregorianCalendar.SEPTEMBER): System.out.print("September ");break; |
|||
case(GregorianCalendar.OCTOBER): System.out.print("October ");break; |
|||
case(GregorianCalendar.NOVEMBER): System.out.print("November ");break; |
|||
default: System.out.print("December ");break; |
|||
} |
|||
⚫ | |||
} |
} |
||
} |
} |
Revision as of 04:44, 15 February 2008
You are encouraged to solve this task according to the task description, using any language you may know.
Display a given date in the formats of "2007-11-10" and "Sunday, November 10, 2007".
Ada
[[Category:{{{1}}} examples needing attention]]Property "Example requires attention" (as page type) with input value "{{{1}}}" contains invalid characters or is incomplete and therefore can cause unexpected results during a query or annotation process.
with Ada.Calendar; use Ada.Calendar; with Ada.Calendar.Arithmetic; use Ada.Calendar.Arithmetic; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with Ada.Text_Io; use Ada.Text_IO; procedure Date_Format is type Month_Names is array(Month_Number) of Unbounded_String; Months : constant Month_Names := ( To_Unbounded_String("January"), To_Unbounded_String("February"), To_Unbounded_String("March"), To_Unbounded_String("April"), To_Unbounded_String("May"), To_Unbounded_String("June"), To_Unbounded_String("July"), To_Unbounded_String("August"), To_Unbounded_String("September"), To_Unbounded_String("October"), To_Unbounded_String("November"), To_Unbounded_String("December")); The_Day : Time; Increment : constant Day_Count := 1; begin The_Day := Clock; Put_Line("Today is: " & To_String(Months(Month(The_Day))) & Day_Number'Image(Day(The_Day)) & "," & Year_Number'Image(Year(The_Day))); The_Day := The_Day + Increment; Put_Line("Tomorrow is: " & To_String(Months(Month(The_Day))) & Day_Number'Image(Day(The_Day)) & "," & Year_Number'Image(Year(The_Day))); end Date_Format;
D
Compiler: D - DMD 1.026
module datetimedemo ; import tango.time.Time ; import tango.text.locale.Locale ; import tango.time.chrono.Gregorian ; import tango.io.Stdout ; void main() { Gregorian g = new Gregorian ; Stdout.layout = new Locale; // enable Stdout to handle date/time format Time d = g.toTime(2007, 11, 10, 0, 0, 0, 0, g.AD_ERA) ; Stdout.format("{:yyy-MM-dd}", d).newline ; Stdout.format("{:dddd, MMMM d, yyy}", d).newline ; d = g.toTime(2008, 2, 1, 0, 0, 0, 0, g.AD_ERA) ; Stdout.format("{:dddd, MMMM d, yyy}", d).newline ; }
Sample Output:
2007-11-10 Saturday, November 10, 2007 Friday, February 1, 2008
Forth
: str-table create ( n -- ) 0 do , loop does> ( n -- str len ) swap cells + @ count ; here ," December" here ," November" here ," October" here ," September" here ," August" here ," July" here ," June" here ," May" here ," April" here ," March" here ," February" here ," January" 12 str-table months : .long-date time&date ( s m h D M Y ) >R 1- months type space 1 .r [char] , emit space R> . drop drop drop ;
: .-0 ( n -- n ) [char] - emit dup 10 < if [char] 0 emit then ; : .short-date time&date 1 .r .-0 1 .r .-0 1 .r drop drop drop ;
J
dayNm =: ;:'Sunday Monday Tuesday Wednesday Thursday Friday Saturday' monthNm=: ;:'0 January February March April May June July August September October November December' dateFormat=: 3 : 0 0 dateFormat y NB. default to short format : 'year month date'=. 3{. y select. x case. 0 do. NB. short format YYYY-MM-DD, e.g. '1900-02-28' dashNN=. (('-0' {.~ (3-#)) ,])@ ": (":year),(dashNN month),(dashNN date) case. 1 do. NB. verbose format, e.g. 'Wednesday, February 28, 1900' Y=. year - A =. <. 12%~ 14-month M=. <.12%~31*(month+12*A)-2 D=. 7| date+M+Y+(<.Y%4)+(<.Y%400)-(<.Y%100) (>D{dayNm), ', ',(>month{monthNm),' ',(":date), ', ', ":year end. )
Example:
(dateFormat ,: 1& dateFormat) 1920 12 17 1920-12-17 Friday, December 17, 1920
Java
import java.util.Calendar; import java.util.GregorianCalendar; import java.text.DateFormatSymbols; public class Dates{ public static void main(String[] args){ Calendar now = new GregorianCalendar(); //months are 0 indexed, dates are 1 indexed DateFormatSymbols symbols = new DateFormatSymbols(); //names for our months and weekdays //plain numbers way System.out.println(now.get(Calendar.YEAR) + "-" + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)); //words way System.out.print(symbols.getWeekdays()[now.get(Calendar.DAY_OF_WEEK)] + ", "); System.out.print(symbols.getMonths()[now.get(Calendar.MONTH)] + " "); System.out.println(now.get(Calendar.DATE) + ", " + now.get(Calendar.YEAR)); } }
Perl
use POSIX; print POSIX::strftime('%Y-%m-%d', 0, 0, 0, 10, 10, 107), "\n"; print POSIX::strftime('%A, %B %d, %Y', 0, 0, 0, 10, 10, 107), "\n";
Output with locales C:
2007-11-10 Saturday, November 10, 2007
Output with locales cs_CZ.UTF-8:
2007-11-10 Sobota, listopad 10, 2007
Actual date:
use POSIX; print POSIX::strftime('%Y-%m-%d', localtime), "\n"; print POSIX::strftime('%A, %B %d, %Y', localtime), "\n";
Output with locales C:
2008-02-13 Wednesday, February 13, 2008
Python
[[Category:{{{1}}} examples needing attention]]Property "Example requires attention" (as page type) with input value "{{{1}}}" contains invalid characters or is incomplete and therefore can cause unexpected results during a query or annotation process.
Formatting rules: http://docs.python.org/lib/module-time.html (strftime)
import datetime today = datetime.date.today() # This one is built in: print today.isoformat() # Or use a format string for full flexibility: print today.strftime('%A, %B %d, %Y')
Raven
time int as today
Short form:
today '%Y-%m-%d' date
Long form:
today '%A, %B %d, %Y' date