AI2 Component  (Version nb184)
Dates.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
11 
12 import java.util.Date;
13 import java.text.DateFormat;
14 import java.text.ParseException;
15 import java.text.SimpleDateFormat;
16 import java.util.Calendar;
17 import java.util.GregorianCalendar;
18 
23 @SimpleObject
24 public final class Dates {
25 
29  public static final int DATE_YEAR = Calendar.YEAR;
30  public static final int DATE_MONTH = Calendar.MONTH;
31  public static final int DATE_DAY = Calendar.DATE;
32  public static final int DATE_WEEK = Calendar.WEEK_OF_YEAR;
33  public static final int DATE_HOUR = Calendar.HOUR_OF_DAY;
34  public static final int DATE_MINUTE = Calendar.MINUTE;
35  public static final int DATE_SECOND = Calendar.SECOND;
36  public static final int DATE_MILLISECOND = Calendar.MILLISECOND;
37 
41  public static final int DATE_MONDAY = Calendar.MONDAY;
42  public static final int DATE_TUESDAY = Calendar.TUESDAY;
43  public static final int DATE_WEDNESDAY = Calendar.WEDNESDAY;
44  public static final int DATE_THURSDAY = Calendar.THURSDAY;
45  public static final int DATE_FRIDAY = Calendar.FRIDAY;
46  public static final int DATE_SATURDAY = Calendar.SATURDAY;
47  public static final int DATE_SUNDAY = Calendar.SUNDAY;
48 
52  public static final int DATE_JANUARY = Calendar.JANUARY;
53  public static final int DATE_FEBRUARY = Calendar.FEBRUARY;
54  public static final int DATE_MARCH = Calendar.MARCH;
55  public static final int DATE_APRIL = Calendar.APRIL;
56  public static final int DATE_MAY = Calendar.MAY;
57  public static final int DATE_JUNE = Calendar.JUNE;
58  public static final int DATE_JULY = Calendar.JULY;
59  public static final int DATE_AUGUST = Calendar.AUGUST;
60  public static final int DATE_SEPTEMBER = Calendar.SEPTEMBER;
61  public static final int DATE_OCTOBER = Calendar.OCTOBER;
62  public static final int DATE_NOVEMBER = Calendar.NOVEMBER;
63  public static final int DATE_DECEMBER = Calendar.DECEMBER;
64 
65  private Dates() { // COV_NF_LINE
66  } // COV_NF_LINE
67 
79  public static void DateAdd(Calendar date, int intervalKind, int interval) {
80  switch (intervalKind) {
81  default:
82  throw new IllegalArgumentException("illegal date/time interval kind in function DateAdd()");
83 
84  case DATE_YEAR:
85  case DATE_MONTH:
86  case DATE_DAY:
87  case DATE_WEEK:
88  case DATE_HOUR:
89  case DATE_MINUTE:
90  case DATE_SECOND:
91  date.add(intervalKind, interval);
92  break;
93  }
94  }
95 
106  public static void DateAddInMillis(Calendar date, long millis) {
107  long dateInMillis = date.getTimeInMillis();
108  date.setTimeInMillis(dateInMillis+millis);
109  }
110 
128  public static Calendar DateValue(String value) {
129  Calendar date = new GregorianCalendar();
130  date.setTime(tryParseDate(value));
131  return date;
132  }
133 
134  private static Date tryParseDate(String value) {
135  String[] formats = {
136  "MM/dd/yyyy hh:mm:ss a",
137  "MM/dd/yyyy HH:mm:ss",
138  "MM/dd/yyyy hh:mm a",
139  "MM/dd/yyyy HH:mm",
140  "MM/dd/yyyy",
141  "hh:mm:ss a",
142  "HH:mm:ss",
143  "hh:mm a",
144  "HH:mm"
145  };
146  for (String format : formats) {
147  try {
148  return new SimpleDateFormat(format).parse(value);
149  } catch (ParseException e) {}
150  }
151  throw new IllegalArgumentException("illegal date/time format in function DateValue()");
152  }
153 
160  @SimpleFunction
161  public static int Day(Calendar date) {
162  return date.get(Calendar.DAY_OF_MONTH);
163  }
164 
174  public static long ConvertDuration(long duration, int intervalKind) {
175  switch (intervalKind) {
176  default:
177  throw new IllegalArgumentException("illegal date/time interval kind in function Duration()");
178  case DATE_WEEK:
179  return duration/1000/60/60/24/7;
180  case DATE_DAY:
181  return duration/1000/60/60/24;
182  case DATE_HOUR:
183  return duration/1000/60/60;
184  case DATE_MINUTE:
185  return duration/1000/60;
186  case DATE_SECOND:
187  return duration/1000;
188  }
189  }
190 
201  public static String FormatDateTime(Calendar date, String pattern) {
202  SimpleDateFormat formatdate = new SimpleDateFormat();
203  if (pattern.length() == 0) {
204  formatdate.applyPattern("MMM d, yyyy hh:mm:ss a");
205  } else {
206  formatdate.applyPattern(pattern);
207  }
208  return formatdate.format(date.getTime());
209  }
210 
221  public static String FormatDate(Calendar date, String pattern) {
222  SimpleDateFormat formatdate = new SimpleDateFormat();
223  if (pattern.length() == 0) {
224  formatdate.applyPattern("MMM d, yyyy");
225  } else {
226  formatdate.applyPattern(pattern);
227  }
228  return formatdate.format(date.getTime());
229  }
230 
240  public static String FormatTime(Calendar date) {
241  return DateFormat.getTimeInstance(DateFormat.MEDIUM).format(date.getTime());
242  }
243 
253  public static Calendar DateInstant(int year, int month, int day) {
254  String year_str = String.valueOf(year);
255  String month_str = String.valueOf(month);
256  String day_str = String.valueOf(day);
257  String date;
258  if (month < 10)
259  month_str = "0" + month_str;
260  if (day < 10)
261  day_str = "0" + day_str;
262  date = month_str + "/" + day_str + "/" + year_str;
263  return Dates.DateValue(date);
264  }
265 
275  public static Calendar TimeInstant(int hour, int minute) {
276  String hour_str = String.valueOf(hour);
277  String minute_str = String.valueOf(minute);
278  String time;
279  if (hour < 10)
280  hour_str = "0" + hour_str;
281  if (minute < 10)
282  minute_str = "0" + minute_str;
283  time = hour_str + ":" + minute_str;
284  return Dates.DateValue(time);
285  }
286 
294  public static int Hour(Calendar date) {
295  return date.get(Calendar.HOUR_OF_DAY);
296  }
297 
305  public static int Minute(Calendar date) {
306  return date.get(Calendar.MINUTE);
307  }
308 
321  public static int Month(Calendar date) {
322  return date.get(Calendar.MONTH);
323  }
324 
332  public static String MonthName(Calendar date) {
333  return String.format("%1$tB", date);
334  }
335 
342  public static Calendar Now() {
343  return new GregorianCalendar();
344  }
345 
353  public static int Second(Calendar date) {
354  return date.get(Calendar.SECOND);
355  }
356 
363  public static long Timer() {
364  return System.currentTimeMillis();
365  }
366 
377  public static int Weekday(Calendar date) {
378  return date.get(Calendar.DAY_OF_WEEK);
379  }
380 
388  public static String WeekdayName(Calendar date) {
389  return String.format("%1$tA", date);
390  }
391 
399  public static int Year(Calendar date) {
400  return date.get(Calendar.YEAR);
401  }
402 }
com.google.appinventor.components.runtime.util.Dates.ConvertDuration
static long ConvertDuration(long duration, int intervalKind)
Definition: Dates.java:174
com.google.appinventor.components.runtime.util.Dates.DATE_TUESDAY
static final int DATE_TUESDAY
Definition: Dates.java:42
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.util.Dates.FormatTime
static String FormatTime(Calendar date)
Definition: Dates.java:240
com.google.appinventor.components.runtime.util.Dates.Timer
static long Timer()
Definition: Dates.java:363
com.google.appinventor.components.runtime.util.Dates.Second
static int Second(Calendar date)
Definition: Dates.java:353
com.google.appinventor.components.runtime.util.Dates.DATE_OCTOBER
static final int DATE_OCTOBER
Definition: Dates.java:61
com.google.appinventor.components.runtime.util.Dates.DATE_JANUARY
static final int DATE_JANUARY
Definition: Dates.java:52
com.google.appinventor.components.runtime.util.Dates.TimeInstant
static Calendar TimeInstant(int hour, int minute)
Definition: Dates.java:275
com.google.appinventor.components.runtime.util.Dates.DATE_AUGUST
static final int DATE_AUGUST
Definition: Dates.java:59
com.google.appinventor.components.runtime.util.Dates.FormatDateTime
static String FormatDateTime(Calendar date, String pattern)
Definition: Dates.java:201
com.google.appinventor.components.runtime.util.Dates.DateAdd
static void DateAdd(Calendar date, int intervalKind, int interval)
Definition: Dates.java:79
com.google.appinventor.components
com.google.appinventor.components.runtime.util.Dates.DATE_YEAR
static final int DATE_YEAR
Definition: Dates.java:29
com.google.appinventor.components.runtime.util.Dates.DATE_WEEK
static final int DATE_WEEK
Definition: Dates.java:32
com.google.appinventor.components.runtime.util.Dates.DATE_WEDNESDAY
static final int DATE_WEDNESDAY
Definition: Dates.java:43
com.google.appinventor.components.runtime.util.Dates.Year
static int Year(Calendar date)
Definition: Dates.java:399
com.google.appinventor.components.runtime.util.Dates.DATE_SUNDAY
static final int DATE_SUNDAY
Definition: Dates.java:47
com.google.appinventor.components.runtime.util.Dates.Day
static int Day(Calendar date)
Definition: Dates.java:161
com.google.appinventor.components.runtime.util.Dates.DATE_JUNE
static final int DATE_JUNE
Definition: Dates.java:57
com.google.appinventor.components.runtime.util.Dates.Weekday
static int Weekday(Calendar date)
Definition: Dates.java:377
com.google.appinventor.components.runtime.util.Dates.DATE_SEPTEMBER
static final int DATE_SEPTEMBER
Definition: Dates.java:60
com.google.appinventor.components.runtime.util.Dates.DATE_MAY
static final int DATE_MAY
Definition: Dates.java:56
com.google.appinventor.components.runtime.util.Dates.DATE_DAY
static final int DATE_DAY
Definition: Dates.java:31
com.google.appinventor.components.runtime.util.Dates.DATE_MONTH
static final int DATE_MONTH
Definition: Dates.java:30
com.google.appinventor.components.runtime.util.Dates.DATE_THURSDAY
static final int DATE_THURSDAY
Definition: Dates.java:44
com.google.appinventor.components.runtime.util.Dates.DATE_FEBRUARY
static final int DATE_FEBRUARY
Definition: Dates.java:53
com.google.appinventor.components.runtime.util.Dates.MonthName
static String MonthName(Calendar date)
Definition: Dates.java:332
com.google.appinventor.components.runtime.util.Dates.DATE_HOUR
static final int DATE_HOUR
Definition: Dates.java:33
com.google.appinventor.components.runtime.util.Dates.DATE_FRIDAY
static final int DATE_FRIDAY
Definition: Dates.java:45
com.google.appinventor.components.runtime.util.Dates.FormatDate
static String FormatDate(Calendar date, String pattern)
Definition: Dates.java:221
com.google.appinventor.components.runtime.util.Dates.DATE_MILLISECOND
static final int DATE_MILLISECOND
Definition: Dates.java:36
com.google.appinventor.components.runtime.util.Dates.DateValue
static Calendar DateValue(String value)
Definition: Dates.java:128
com.google.appinventor.components.runtime.util.Dates
Definition: Dates.java:24
com.google.appinventor.components.runtime.util.Dates.DATE_APRIL
static final int DATE_APRIL
Definition: Dates.java:55
com.google.appinventor.components.runtime.util.Dates.DATE_MONDAY
static final int DATE_MONDAY
Definition: Dates.java:41
com.google.appinventor.components.runtime.util.Dates.DATE_MARCH
static final int DATE_MARCH
Definition: Dates.java:54
com.google.appinventor.components.runtime.util.Dates.DATE_SECOND
static final int DATE_SECOND
Definition: Dates.java:35
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.util.Dates.Month
static int Month(Calendar date)
Definition: Dates.java:321
com.google
com
com.google.appinventor.components.runtime.util.Dates.Now
static Calendar Now()
Definition: Dates.java:342
com.google.appinventor.components.runtime.util.Dates.DATE_NOVEMBER
static final int DATE_NOVEMBER
Definition: Dates.java:62
com.google.appinventor.components.runtime.util.Dates.Minute
static int Minute(Calendar date)
Definition: Dates.java:305
com.google.appinventor.components.runtime.util.Dates.WeekdayName
static String WeekdayName(Calendar date)
Definition: Dates.java:388
com.google.appinventor.components.runtime.util.Dates.DateAddInMillis
static void DateAddInMillis(Calendar date, long millis)
Definition: Dates.java:106
com.google.appinventor.components.runtime.util.Dates.DATE_MINUTE
static final int DATE_MINUTE
Definition: Dates.java:34
com.google.appinventor.components.runtime.util.Dates.DATE_DECEMBER
static final int DATE_DECEMBER
Definition: Dates.java:63
com.google.appinventor.components.runtime.util.Dates.DATE_SATURDAY
static final int DATE_SATURDAY
Definition: Dates.java:46
com.google.appinventor.components.annotations
com.google.appinventor
com.google.appinventor.components.runtime.util.Dates.DATE_JULY
static final int DATE_JULY
Definition: Dates.java:58
com.google.appinventor.components.runtime.util.Dates.DateInstant
static Calendar DateInstant(int year, int month, int day)
Definition: Dates.java:253
com.google.appinventor.components.runtime.util.Dates.Hour
static int Hour(Calendar date)
Definition: Dates.java:294