Search in sources :

Example 6 with DateFormat

use of com.codename1.l10n.DateFormat in project CodenameOne by codenameone.

the class SimpleDateFormatTests method runTest.

@Override
public boolean runTest() throws Exception {
    if (Display.getInstance().isSimulator()) {
        NativeTimeZoneUtil tzUtil = NativeLookup.create(NativeTimeZoneUtil.class);
        TimeZone defaultTz = TimeZone.getDefault();
        try {
            for (String tzName : new String[] { "America/Vancouver", "America/Chicago", "UTC" }) {
                tzUtil.setDefaultTimeZone(tzName);
                String[] data = new String[] { "yyyy-MM-dd kk:mm:ss.SSS", "2018-04-26 08:04:30.511", "h:mm aa", "8:04 AM", "yyyy-MM-dd hh:mm aaa, z", "2018-04-26 08:04 PM, PDT", "h:mm aa", "8:04 PM", "yyyy-MM-dd hh:mm aaa, z", "2018-04-26 08:04 PM, GMT-08:00", "h:mm aa", "5:04 PM", "yyyy-MM-dd hh:mm aaa, z", "2018-04-26 08:04 PM, GMT", "h:mm aa", "5:04 PM", // ,
                "dd-MMM-yy HH:mm:ss z", // ,
                "26-aug-20 18:02:09 gmt" // "EEE, dd-MMM-yy HH:mm:ss z", "wed, 26-aug-20 18:02:09 gmt"
                };
                for (int i = 0; i < data.length; i += 4) {
                    java.text.DateFormat messageDateFormat0 = new java.text.SimpleDateFormat(data[i], java.util.Locale.US);
                    SimpleDateFormat messageDateFormat = new SimpleDateFormat(data[i]);
                    messageDateFormat.getDateFormatSymbols().addZoneMapping("America/Vancouver", "Pacific Standard Time", "Pacific Daylight Time", "PST", "PDT");
                    messageDateFormat.getDateFormatSymbols().addZoneMapping("America/New_York", "Eastern Standard Time", "Eastern Daylight Time", "EST", "EDT");
                    messageDateFormat.getDateFormatSymbols().addZoneMapping("America/Chicago", "Central Standard Time", "Central Daylight Time", "CST", "CDT");
                    Date when0 = messageDateFormat0.parse(data[i + 1]);
                    Date when = messageDateFormat.parse(data[i + 1]);
                    assertEqual(when0, when, "In timezone " + tzName + ", SimpleDateFormat parse deviated from java.text version.  Parsing " + data[i + 1] + " with format " + data[i]);
                    // if (!when0.equals(when)) {
                    // throw new RuntimeException("Test "+(i/4)+" FAILED.  Expected "+when0+" but found "+when);
                    // }
                    // What is date that is associated with when?
                    DateFormat displayTimeFormat = messageDateFormat;
                    java.text.DateFormat displayTimeFormat0 = new java.text.SimpleDateFormat(data[i], java.util.Locale.US);
                    String output0 = displayTimeFormat0.format(when);
                    String output = displayTimeFormat.format(when);
                    assertEqual(output0, output, "In timezone " + tzName + " SimpleDateFormat format deviated from java.text version.  Formatting " + data[i + 1] + " with format " + data[i]);
                }
            }
        } finally {
            tzUtil.setDefaultTimeZone(defaultTz.getID());
        }
    }
    /*
        if (true) {
            long[] timestamps = new long[]{
                1524755070511L, 1524798240000L, 1524801840000L, 1524773040000L
            };
            String[] data = new String[]{
                "yyyy-MM-dd kk:mm:ss.SSS", "2018-04-26 08:04:30.511 PDT", "h:mm aa", "8:04 AM",
                "yyyy-MM-dd hh:mm aaa, z", "2018-04-26 08:04 PM, PDT", "h:mm aa", "8:04 PM",
                "yyyy-MM-dd hh:mm aaa, z", "2018-04-26 08:04 PM, GMT-08:00", "h:mm aa", "5:04 PM",
                "yyyy-MM-dd hh:mm aaa, z", "2018-04-26 08:04 PM, GMT", "h:mm aa", "5:04 PM"

            };
            for (int i=0; i<data.length; i+=4) {
                //java.text.DateFormat messageDateFormat0 = new java.text.SimpleDateFormat(data[i]);
                SimpleDateFormat messageDateFormat = new SimpleDateFormat(data[i]);
                messageDateFormat.getDateFormatSymbols().addZoneMapping("America/Vancouver", "Pacific Standard Time", "Pacific Daylight Time", "PST", "PDT");
                messageDateFormat.getDateFormatSymbols().addZoneMapping("America/New_York", "Eastern Standard Time", "Eastern Daylight Time", "EST", "EDT");
                //Date when0 = messageDateFormat0.parse(data[i+1]);
                Date when = messageDateFormat.parse(data[i+1]);
                assertEqual(timestamps[i/4], when.getTime(), "SimpleDateFormat parse deviated from java.text version.  Parsing "+data[i+1]+" with format "+data[i]);
                
                // Formatting depends on the timezone, so we won't run these tests as they will turn out
                // differently depending on the timezone of the device.
                //if (!when0.equals(when)) {
                //    throw new RuntimeException("Test "+(i/4)+" FAILED.  Expected "+when0+" but found "+when);
                //}
                // What is date that is associated with when?
                //DateFormat displayTimeFormat = new SimpleDateFormat(data[i+2]);
                //java.text.DateFormat displayTimeFormat0 = new java.text.SimpleDateFormat(data[i+2]);
                //String output0 = displayTimeFormat0.format(when);
                //String output = displayTimeFormat.format(when);
                //assertEqual(output0, output, "SimpleDateFormat format deviated from java.text version.  Formatting "+data[i+1]+" with format "+data[i+2]);

            }
        }
                */
    return true;
}
Also used : TimeZone(java.util.TimeZone) DateFormat(com.codename1.l10n.DateFormat) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat) NativeTimeZoneUtil(com.codename1.ui.tests.NativeTimeZoneUtil) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat) Date(java.util.Date)

Example 7 with DateFormat

use of com.codename1.l10n.DateFormat in project CodenameOne by codenameone.

the class CameraKitPickerTest7 method showAnotherForm.

public void showAnotherForm() {
    Form form = new Form(new BorderLayout(BorderLayout.CENTER_BEHAVIOR_SCALE));
    form.setScrollable(false);
    Container container = new Container(BoxLayout.y());
    container.add(new Label("Test", "heading5"));
    container.add(new Label("", "newLine"));
    container.add(new Label("", "newLine"));
    final Picker fromDate = new Picker();
    fromDate.setType(Display.PICKER_TYPE_DATE);
    container.add(new Label("From Date", "heading7"));
    container.add(fromDate);
    container.add(new Label("", "newLine"));
    final Picker toDate = new Picker();
    toDate.setType(Display.PICKER_TYPE_DATE);
    container.add(new Label("To Date", "heading7"));
    container.add(toDate);
    Button nextBtn = new Button("Proceed");
    nextBtn.setUIID("loginBtn");
    nextBtn.addActionListener((ev) -> {
        DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
        String fromDateStr = dateFormat.format(fromDate.getDate());
        String toDateStr = dateFormat.format(toDate.getDate());
    });
    container.add(new Label("", "newLine"));
    container.add(new Label("", "newLine"));
    container.add(nextBtn);
    form.add(BorderLayout.CENTER, container);
    form.show();
}
Also used : Container(com.codename1.ui.Container) BorderLayout(com.codename1.ui.layouts.BorderLayout) Form(com.codename1.ui.Form) Button(com.codename1.ui.Button) FloatingActionButton(com.codename1.components.FloatingActionButton) Picker(com.codename1.ui.spinner.Picker) DateFormat(com.codename1.l10n.DateFormat) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat) SpanLabel(com.codename1.components.SpanLabel) Label(com.codename1.ui.Label) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat)

Aggregations

Date (java.util.Date)4 DateFormat (com.codename1.l10n.DateFormat)3 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)3 DateFormat (java.text.DateFormat)3 L10NManager (com.codename1.l10n.L10NManager)2 Button (com.codename1.ui.Button)2 Container (com.codename1.ui.Container)2 Form (com.codename1.ui.Form)2 Label (com.codename1.ui.Label)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Locale (java.util.Locale)2 Paint (android.graphics.Paint)1 Paint (com.codename1.charts.compat.Paint)1 FloatingActionButton (com.codename1.components.FloatingActionButton)1 SpanLabel (com.codename1.components.SpanLabel)1 ParseException (com.codename1.l10n.ParseException)1 TextField (com.codename1.ui.TextField)1 BorderLayout (com.codename1.ui.layouts.BorderLayout)1 Picker (com.codename1.ui.spinner.Picker)1