Unverified Commit edaebb9e authored by Farzeen Harunani's avatar Farzeen Harunani Committed by Jeffrey I. Schiller

Added more flexibility to DateValue and FormatDateTime

Fixes #1204. Also fix some typos in the documentation.

Change-Id: I7811a2ed368478e3306c9f54ee347f7adfd33fea
parent eda075ad
......@@ -1111,7 +1111,7 @@ public interface OdeMessages extends Messages {
String horizontalAlignmentChoiceLeft();
@DefaultMessage("Right")
@Description("Text for horizontal alignemt choice 'Right'")
@Description("Text for horizontal alignment choice 'Right'")
String horizontalAlignmentChoiceRight();
@DefaultMessage("Center")
......@@ -5841,9 +5841,9 @@ public interface OdeMessages extends Messages {
+ "regularly set intervals and perform time calculations, "
+ "manipulations, and conversions.</p> <p>Methods to convert an "
+ "instant to text are also available. Acceptable patterns are "
+ "empty string, MM/DD/YYYY HH:mm:ss a, or MMM d, yyyy "
+ "empty string, MM/dd/YYYY hh:mm:ss a, or MMM d, yyyy "
+ "HH:mm. The empty string will provide the default format, "
+ "which is \"MMM d, yyyy HH:mm:ss a\" for FormatDateTime \"MMM "
+ "which is \"MMM d, yyyy hh:mm:ss a\" for FormatDateTime \"MMM "
+ "d, yyyy\" for FormatDate. To see all possible format, "
+ "please see <a "
+ "href=\"https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html\" "
......@@ -6247,7 +6247,7 @@ public interface OdeMessages extends Messages {
@DefaultMessage("<p>It appears that <b>" + "%1" +
"</b> has had all blocks removed.</p><p>" +
"<ul><li>You can save the enpty screen, and then all those blocks will be " +
"<ul><li>You can save the empty screen, and then all those blocks will be " +
"permanently gone from the project.</li>" +
"<li>Alternatively, you can restore the previously saved version " +
"of the project.</li></ul></p>")
......
......@@ -196,13 +196,13 @@ public final class Clock extends AndroidNonvisibleComponent
* @return date
*/
@SimpleFunction(
description = "An instant in time specified by MM/DD/YYYY hh:mm:ss or MM/DD/YYYY or hh:mm")
description = "An instant in time specified by MM/dd/YYYY hh:mm:ss or MM/dd/YYYY or hh:mm")
public static Calendar MakeInstant(String from) {
try {
return Dates.DateValue(from);
} catch (IllegalArgumentException e) {
throw new YailRuntimeError(
"Argument to MakeInstant should have form MM/DD/YYYY hh:mm:ss, or MM/DD/YYYY or hh:mm",
"Argument to MakeInstant should have form MM/dd/YYYY hh:mm:ss, or MM/dd/YYYY or hh:mm",
"Sorry to be so picky.");
}
}
......@@ -459,7 +459,7 @@ public final class Clock extends AndroidNonvisibleComponent
* Converts and formats an instant into a string of date and time with the specified pattern. *
*
* @param instant instant to format
* @param pattern format of the date and time e.g. MM/DD/YYYY HH:mm:ss a, MMM d, yyyy HH:mm
* @param pattern format of the date and time e.g. MM/dd/YYYY hh:mm:ss a, MMM d, yyyy HH:mm
* @return formatted instant
*/
@SimpleFunction (description = "Text representing the date and time of an"
......@@ -469,7 +469,7 @@ public final class Clock extends AndroidNonvisibleComponent
return Dates.FormatDateTime(instant, pattern);
} catch (IllegalArgumentException e){
throw new YailRuntimeError(
"Illegal argument for pattern in Clock.FormatDateTime. Acceptable values are empty string, MM/DD/YYYY HH:mm:ss a, MMM d, yyyy HH:mm "
"Illegal argument for pattern in Clock.FormatDateTime. Acceptable values are empty string, MM/dd/YYYY hh:mm:ss a, MMM d, yyyy HH:mm "
+ "For all possible patterns, see https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html",
"Sorry to be so picky.");
}
......
......@@ -9,6 +9,7 @@ package com.google.appinventor.components.runtime.util;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
......@@ -126,26 +127,28 @@ public final class Dates {
@SimpleFunction
public static Calendar DateValue(String value) {
Calendar date = new GregorianCalendar();
try {
DateFormat dateTimeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
dateTimeFormat.setLenient(true);
date.setTime(dateTimeFormat.parse(value));
} catch (ParseException e) {
date.setTime(tryParseDate(value));
return date;
}
private static Date tryParseDate(String value) {
String[] formats = {
"MM/dd/yyyy hh:mm:ss a",
"MM/dd/yyyy HH:mm:ss",
"MM/dd/yyyy hh:mm a",
"MM/dd/yyyy HH:mm",
"MM/dd/yyyy",
"hh:mm:ss a",
"HH:mm:ss",
"hh:mm a",
"HH:mm"
};
for (String format : formats) {
try {
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
dateFormat.setLenient(true);
date.setTime(dateFormat.parse(value));
} catch (ParseException e1) {
try {
DateFormat dateFormat = new SimpleDateFormat("HH:mm");
dateFormat.setLenient(true);
date.setTime(dateFormat.parse(value));
} catch (ParseException pe) {
throw new IllegalArgumentException("illegal date/time format in function DateValue()");
}
}
return new SimpleDateFormat(format).parse(value);
} catch (ParseException e) {}
}
return date;
throw new IllegalArgumentException("illegal date/time format in function DateValue()");
}
/**
......@@ -191,14 +194,14 @@ public final class Dates {
* @see SimpleDateFormat
*
* @param date date to format
* @param pattern format of the date and time e.g. MM/DD/YYYY HH:mm:ss a, MMM d, yyyy HH:mm
* @param pattern format of the date and time e.g. MM/dd/YYYY hh:mm:ss a, MMM d, yyyy HH:mm
* @return formatted date
*/
@SimpleFunction
public static String FormatDateTime(Calendar date, String pattern) {
SimpleDateFormat formatdate = new SimpleDateFormat();
if (pattern.length() == 0) {
formatdate.applyPattern("MMM d, yyyy HH:mm:ss a");
formatdate.applyPattern("MMM d, yyyy hh:mm:ss a");
} else {
formatdate.applyPattern(pattern);
}
......
......@@ -539,7 +539,7 @@ none
<h2 id="Clock"> Clock </h2>
<img src="images/clock.png" alt="" />
<p>Non-visible component that provides the instant in time using the internal clock on the phone.It can fire a timer at regularly set intervals and perform time calculations, manipulations, and conversions.</p>
<p>Methods to convert an instant to text are also available. Acceptable patterns are empty string, MM/DD/YYYY HH:mm:ss a, or MMM d, yyyy HH:mm. The empty string will provide the default format, which is "MMM d, yyyy HH:mm:ss a" for FormatDateTime, "MMM d, yyyy" for FormatDate. To see all possible format, please see <a href="https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html">here</a>. </p>
<p>Methods to convert an instant to text are also available. Acceptable patterns are empty string, MM/dd/YYYY HH:mm:ss a, or MMM d, yyyy HH:mm. The empty string will provide the default format, which is "MMM d, yyyy HH:mm:ss a" for FormatDateTime, "MMM d, yyyy" for FormatDate. To see all possible format, please see <a href="https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html">here</a>. </p>
<p> Date and Time are formatted with InstantInTime and Duration. </p>
<ul>
<li> <strong> Instant </strong> : consists of Year, Month, DayOfMonth, Hour, Minute, and Second.
......@@ -604,7 +604,7 @@ none
<dt> <code> number Hour(InstantInTime instant) </code> </dt>
<dd> Returns the hour of the day(0-23) from the instant </dd>
<dt> <code> InstantInTime MakeInstant(text from) </code> </dt>
<dd> Returns an instant specified by MM/DD/YYYY hh:mm:ss or MM/DD/YYYY or hh:mm. An example text input is "06/22/2015 12:18"</dd>
<dd> Returns an instant specified by MM/dd/YYYY hh:mm:ss or MM/dd/YYYY or hh:mm. An example text input is "06/22/2015 12:18"</dd>
<dt> <code> InstantInTime MakeInstantFromMillis(number millis) </code> </dt>
<dd> Returns an instant in time specified by the milliseconds since 1970. </dd>
<dt> <code> number Minute(InstantInTime instant) </code> </dt>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment