DateFormat format - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for DateFormat format method.

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: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: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
This code example shows how to use the following methods:format, setCalendar
   } 
 
   @Test 
   public void testDate() { 
       Date d = new Date();
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
       format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
       String formattedDate = format.format(d);
 
       String serialized = JSON.serialize(d);
       assertEquals("{ \"$date\" : \"" + formattedDate + "\"}", serialized);
 
       Date d2 = (Date)JSON.parse(serialized);
       assertEquals(d.toString(), d2.toString());
       assertTrue(d.equals(d2));
   } 
 
    @Test 
    public void testJSONEncoding() throws ParseException { 
        String json = "{ 'str' : 'asdfasd' , 'long' : 123123123123 , 'int' : 5 , 'float' : 0.4 , 'bool' : false , 'date' : { '$date' : '2011-05-18T18:56:00Z'} , 'pat' : { '$regex' : '.*' , '$options' : ''} , 'oid' : { '$oid' : '4d83ab3ea39562db9c1ae2ae'} , 'ref' : { '$ref' : 'test.test' , '$id' : { '$oid' : '4d83ab59a39562db9c1ae2af'}} , 'code' : { '$code' : 'asdfdsa'} , 'codews' : { '$code' : 'ggggg' , '$scope' : { }} , 'ts' : { '$ts' : 1300474885 , '$inc' : 10} , 'null' :  null, 'uuid' : { '$uuid' : '60f65152-6d4a-4f11-9c9b-590b575da7b5' }}";
8
CodeRank
This code example shows how to use the following methods:format
		calendar.add(field, amount);
		return this;
	} 
	 
	public String format(String pattern){
		SimpleDateFormat format=new SimpleDateFormat(pattern);
		return format.format(calendar.getTime());
	} 
	 
	public static void main(String[] args) {
		String v=new ZeusDateTool().add(Calendar.DAY_OF_MONTH,-1000).format("yyyy-MM-dd");
		System.out.println(v);
	} 
} 
8
CodeRank
This code example shows how to use the following methods:format, setTimeZone, applyPattern
                    position.getIndex() == end);
        } 
 
        public void verifyFormatTimezone(String timeZoneId, String expected1,
                String expected2, Date date) {
            format.setTimeZone(SimpleTimeZone.getTimeZone(timeZoneId));
            format.applyPattern("z, zzzz");
            assertEquals("Test z for TimeZone : " + timeZoneId, expected1,
                    format.format(date));
 
            format.applyPattern("Z, ZZZZ");
            assertEquals("Test Z for TimeZone : " + timeZoneId, expected2,
                    format.format(date));
        } 
    } 
 
    /** 
     * java.text.SimpleDateFormat#SimpleDateFormat(java.lang.String, 
     *        java.text.DateFormatSymbols) 
     */ 
8
CodeRank
This code example shows how to use the following methods:format
        if (args.length >= 1) {
            Object o1 = args[0];
            if (args.length == 2 && args[1] instanceof String) {
                Object o2 = args[1];
                if (o1 instanceof Calendar || o1 instanceof Date) {
                    DateFormat formatter = new SimpleDateFormat((String) o2);
                    return formatter.format(o1 instanceof Date ? ((Date) o1) : ((Calendar) o1).getTime());
                } else if (o1 instanceof Number) {
                    return String.format((String) o2, (Number) o1);
                } 
            } else if (args.length == 1) {
                if (o1 instanceof String) {
                    return (String) o1;
                } else { 
                    return StringUtils.toString(o1);
                }  
            } 
        } 
        return new EvalError("ToString accepts an object and an optional second argument containing a date format string"); 
    } 
8
CodeRank
This code example shows how to use the following methods:format
   
  public String getFileInfoString() {
    int appSize = failSafeGetIntFromJSON(newest, "appsize", 0);
    long timestamp = failSafeGetIntFromJSON(newest, "timestamp", 0);
    Date date = new Date(timestamp * 1000);
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
    return dateFormat.format(date) + " - " + String.format("%.2f", appSize / 1024F / 1024F) + " MB";
  } 
   
  private static String failSafeGetStringFromJSON(JSONObject json, String name, String defaultValue) {
    try { 
      return json.getString(name);
    } 
    catch (JSONException e) {
      return defaultValue;
    } 
  } 
   
  private static int failSafeGetIntFromJSON(JSONObject json, String name, int defaultValue) {
    try { 
7
CodeRank

Related DateFormat format Questions & Answers:

Other DateFormat format Examples

266:			// Create a media file name
267:			String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
268:					Locale.getDefault()).format(new Date());
269:			File mediaFile;
270:			if (type == MEDIA_TYPE_IMAGE) {

Read More

27:			// Create a media file name
28:			String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
29:					Locale.getDefault()).format(new Date());
30:			File mediaFile;
31:			if (type == MEDIA_TYPE_IMAGE) {

Read More

374:					"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
375:			Date date = new Date();
376:			return dateFormat.format(date);
377:		}
378:	}

Read More

format Method Overview
Overrides Format.
See Code Examples for other Java 8 DateFormat Methods: