7 package com.google.appinventor.components.runtime.util;
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;
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;
56 public static final int DATE_MAY = Calendar.MAY;
79 public static void DateAdd(Calendar date,
int intervalKind,
int interval) {
80 switch (intervalKind) {
82 throw new IllegalArgumentException(
"illegal date/time interval kind in function DateAdd()");
91 date.add(intervalKind, interval);
107 long dateInMillis = date.getTimeInMillis();
108 date.setTimeInMillis(dateInMillis+millis);
129 Calendar date =
new GregorianCalendar();
130 date.setTime(tryParseDate(value));
134 private static Date tryParseDate(String value) {
136 "MM/dd/yyyy hh:mm:ss a",
137 "MM/dd/yyyy HH:mm:ss",
138 "MM/dd/yyyy hh:mm a",
146 for (String format : formats) {
148 return new SimpleDateFormat(format).parse(value);
149 }
catch (ParseException e) {}
151 throw new IllegalArgumentException(
"illegal date/time format in function DateValue()");
161 public static int Day(Calendar date) {
162 return date.get(Calendar.DAY_OF_MONTH);
175 switch (intervalKind) {
177 throw new IllegalArgumentException(
"illegal date/time interval kind in function Duration()");
179 return duration/1000/60/60/24/7;
181 return duration/1000/60/60/24;
183 return duration/1000/60/60;
185 return duration/1000/60;
187 return duration/1000;
202 SimpleDateFormat formatdate =
new SimpleDateFormat();
203 if (pattern.length() == 0) {
204 formatdate.applyPattern(
"MMM d, yyyy hh:mm:ss a");
206 formatdate.applyPattern(pattern);
208 return formatdate.format(date.getTime());
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");
226 formatdate.applyPattern(pattern);
228 return formatdate.format(date.getTime());
241 return DateFormat.getTimeInstance(DateFormat.MEDIUM).format(date.getTime());
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);
259 month_str =
"0" + month_str;
261 day_str =
"0" + day_str;
262 date = month_str +
"/" + day_str +
"/" + year_str;
276 String hour_str = String.valueOf(hour);
277 String minute_str = String.valueOf(minute);
280 hour_str =
"0" + hour_str;
282 minute_str =
"0" + minute_str;
283 time = hour_str +
":" + minute_str;
294 public static int Hour(Calendar date) {
295 return date.get(Calendar.HOUR_OF_DAY);
305 public static int Minute(Calendar date) {
306 return date.get(Calendar.MINUTE);
321 public static int Month(Calendar date) {
322 return date.get(Calendar.MONTH);
333 return String.format(
"%1$tB", date);
342 public static Calendar
Now() {
343 return new GregorianCalendar();
353 public static int Second(Calendar date) {
354 return date.get(Calendar.SECOND);
364 return System.currentTimeMillis();
378 return date.get(Calendar.DAY_OF_WEEK);
389 return String.format(
"%1$tA", date);
399 public static int Year(Calendar date) {
400 return date.get(Calendar.YEAR);