Java 8 DateFormat - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 DateFormat class.

This code example shows how to use the following methods:parse, setLenient
        try { 
            Date date = AnnotationHelper.getDateAs(annotations, value);
            if (date != null) {
                cal.setTime(date);
            } else { 
                SimpleDateFormat sdf = new SimpleDateFormat(I18N.getDateFormat());
                sdf.setLenient(false);
                cal.setTime(sdf.parse(value));
            } 
        } catch (ParseException e) {
            throw new IllegalArgumentException("Cannot convert [" + value + "] to a Calendar: " + e.toString());
        } 
 
        return cal;
    } 
} 
9
CodeRank
This code example shows how to use the following methods:getClass, format, getDateFormatSymbols, toPattern
    } 
 
    public void test_ConstructorLjava_lang_StringLjava_util_Locale() { 
        // Test for method java.text.SimpleDateFormat(java.lang.String, 
        // java.util.Locale) 
        SimpleDateFormat f2 = new SimpleDateFormat("'yyyy' MM yy",
                Locale.GERMAN);
        assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
        assertEquals("Wrong pattern", "'yyyy' MM yy", f2.toPattern());
        assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(
                new DateFormatSymbols(Locale.GERMAN)));
        assertTrue("Doesn't work",
                f2.format(new Date()).getClass() == String.class);
 
        try { 
            new SimpleDateFormat(null, Locale.GERMAN);
            fail("NullPointerException was not thrown.");
        } catch(NullPointerException npe) {
            //expected 
        } 
8
CodeRank
This code example shows how to use the following methods:parse, setLenient
        } catch (ParseException e) {
            // Ignore 
        } 
 
        try { 
            SimpleDateFormat sdf = new SimpleDateFormat(ISO8601);
            sdf.setLenient(false);
            return sdf.parse(value);
        } catch (Exception e) {
            throw new IllegalArgumentException("Cannot convert [" + value + "] to a Date: " + e.toString());
        } 
 
    } 
} 
8
CodeRank
This code example shows how to use the following methods:setCalendar, parse
            o = new ObjectId((String) b.get("$oid"));
        } else if (b.containsField("$date")) {
            if (b.get("$date") instanceof Number) {
                o = new Date(((Number) b.get("$date")).longValue());
            } else { 
                SimpleDateFormat format = new SimpleDateFormat(_msDateFormat);
                format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
                o = format.parse(b.get("$date").toString(), new ParsePosition(0));
 
                if (o == null) {
                    // try older format with no ms 
                    format = new SimpleDateFormat(_secDateFormat);
                    format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
                    o = format.parse(b.get("$date").toString(), new ParsePosition(0));
                } 
            } 
        } else if (b.containsField("$regex")) {
            o = Pattern.compile((String) b.get("$regex"),
                    BSON.regexFlags((String) b.get("$options")));
        } else if (b.containsField("$ts")) { //Legacy timestamp format
8
CodeRank
This code example shows how to use the following methods:format, DateFormat.setTimeZone
        format.setTimeZone(TimeZone.getTimeZone(iParameters[1].toString()));
      else 
        format.setTimeZone(ODateHelper.getDatabaseTimeZone());
    } 
 
    return format.format(now);
  } 
 
  public boolean aggregateResults(final Object[] configuredParameters) {
    return false; 
  } 
 
  public String getSyntax() {
    return "Syntax error: sysdate([<format>] [,<timezone>])"; 
  } 
 
  @Override 
  public Object getResult() {
    return null; 
  } 
8
CodeRank
This code example shows how to use the following methods:format
            } 
        } 
    } 
 
    void log(String s) {
        mLog.setText(mLog.getText() + "\n" + mDateFormat.format(new Date()) + ": " + s);
    } 
 
    BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) { 
            String action = intent.getAction(); 
            String title = action; 
            int index = title.lastIndexOf('.'); 
            if (index >= 0) { 
                title = title.substring(index + 1); 
            } 
            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { 
                int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 
                log(title + ": level=" + level); 
            } else { 
8
CodeRank
This code example shows how to use the following methods:parse, setTimeZone
        format.setTimeZone(ODateHelper.getDatabaseTimeZone());
      } else 
        format = ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getConfiguration().getDateTimeFormatInstance();
 
      if (iParameters.length == 3)
        format.setTimeZone(TimeZone.getTimeZone(iParameters[2].toString()));
    } 
 
    try { 
      return format.parse((String) iParameters[0]);
    } catch (ParseException e) {
      throw new OQueryParsingException("Error on formatting date '" + iParameters[0] + "' using the format: " + format, e);
    } 
  } 
 
  public boolean aggregateResults(final Object[] configuredParameters) {
    return false; 
  } 
 
  public String getSyntax() {
8
CodeRank
This code example shows how to use the following methods:parse
 
    // http://code.google.com/p/android/issues/detail?id=14395 
    public void testPreHistoricInDaylightTime() throws Exception { 
        TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
        TimeZone.setDefault(tz);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        Date date = sdf.parse("1902-11-01T00:00:00.000+0800");
        assertEquals(-2119680000000L, date.getTime());
        assertEquals(-28800000, tz.getOffset(date.getTime()));
        assertFalse(tz.inDaylightTime(date));
        assertEquals("Fri Oct 31 08:00:00 PST 1902", date.toString());
        assertEquals("31 Oct 1902 16:00:00 GMT", date.toGMTString());
        // Any time before we have transition data is considered non-daylight, even in summer. 
        date = sdf.parse("1902-06-01T00:00:00.000+0800");
        assertEquals(-28800000, tz.getOffset(date.getTime()));
        assertFalse(tz.inDaylightTime(date));
    } 
 
    public void testPreHistoricOffsets() throws Exception { 
        // The "Asia/Saigon" time zone has just a few transitions, and hasn't changed in a 
8
CodeRank
This code example shows how to use the following methods:format, setTimeZone
    public void testFormattingUncommonTimeZoneAbbreviations() { 
        // In Honeycomb, only one Olson id was associated with CET (or any 
        // other "uncommon" abbreviation). 
        String fmt = "yyyy-MM-dd HH:mm:ss.SSS z";
        String date = "1970-01-01 01:00:00.000 CET";
        SimpleDateFormat sdf = new SimpleDateFormat(fmt, Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
        assertEquals(date, sdf.format(new Date(0)));
        sdf = new SimpleDateFormat(fmt, Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("Europe/Zurich"));
        assertEquals(date, sdf.format(new Date(0)));
    } 
 
    // http://code.google.com/p/android/issues/detail?id=8258 
    public void testTimeZoneFormatting() throws Exception { 
        Date epoch = new Date(0);
 
        // Create a SimpleDateFormat that defaults to America/Chicago... 
        TimeZone.setDefault(TimeZone.getTimeZone("America/Chicago"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
8
CodeRank
This code example shows how to use the following methods:format, setTimeZone
                return null; 
            } 
        } 
 
        private String toHeader(Date date) {
            SimpleDateFormat httpDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
            httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
            return httpDateFormat.format(date);
        } 
 
        private Date fromHeader(String date) {
            SimpleDateFormat httpDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
            httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
            try { 
                return httpDateFormat.parse(date);
            } catch (Exception ex) {
                return new Date();
            } 
        } 
 
8
CodeRank

Related Java 8 DateFormat Questions & Answers:

See Code Examples for Java 8 DateFormat Methods: