Examples for usage of all methods of 'java.text.DateFormat' with console output of example code.
Please click on method from below list to go to code example for usage of that method. Click [↓ Imports] to get import statements used in examples. To read javadoc of methods, click on [⿺ Javadoc] for that method.
// Format with normal date format
StringBuffer buffer = new StringBuffer("The date is ");
FieldPosition fieldPosition = new FieldPosition(DateFormat.YEAR_FIELD);
StringBuffer formattedDate = DateFormat.getInstance().format(new Date(), buffer, fieldPosition);
System.out.println("formattedDate = " + formattedDate);
System.out.println("fieldPosition = " + fieldPosition);
System.out.println("Year value using field position is = "
+ formattedDate.substring(fieldPosition.getBeginIndex(), fieldPosition.getEndIndex()));
// Format with FULL date format
StringBuffer buffer_1 = new StringBuffer("The date is ");
FieldPosition fieldPosition_1 = new FieldPosition(DateFormat.MONTH_FIELD);
StringBuffer formattedDate_1 = DateFormat.getDateInstance(DateFormat.FULL).format(new Date(), buffer_1,
fieldPosition_1);
System.out.println("\nformattedDate_1 = " + formattedDate_1);
System.out.println("fieldPosition_1 = " + fieldPosition_1);
System.out.println("Year value using field position is = "
+ formattedDate_1.substring(fieldPosition_1.getBeginIndex(), fieldPosition_1.getEndIndex()));
// Format with normal date format
StringBuffer buffer_2 = new StringBuffer("The date is ");
FieldPosition fieldPosition_2 = new FieldPosition(DateFormat.YEAR_FIELD);
// Pass Long in format method. Number & date both are accepted by this method.
Long timeMills = System.currentTimeMillis();
StringBuffer formattedDate_2 = DateFormat.getInstance().format(timeMills, buffer_2, fieldPosition_2);
System.out.println("\nformattedDate_2 = " + formattedDate_2);
System.out.println("fieldPosition_2 = " + fieldPosition_2);
System.out.println("Year value using field position is = "
+ formattedDate.substring(fieldPosition_2.getBeginIndex(), fieldPosition_2.getEndIndex()));
Output: formattedDate = The date is 11/2/19, 11:33 PM fieldPosition = java.text.FieldPosition[field=1,attribute=null,beginIndex=17,endIndex=19] Year value using field position is = 19 formattedDate_1 = The date is Saturday, November 2, 2019 fieldPosition_1 = java.text.FieldPosition[field=2,attribute=null,beginIndex=22,endIndex=30] Year value using field position is = November formattedDate_2 = The date is 11/2/19, 11:33 PM fieldPosition_2 = java.text.FieldPosition[field=1,attribute=null,beginIndex=17,endIndex=19] Year value using field position is = 19
Tag: Example for format method of class java.text.DateFormat., DateFormat format function example with arguments java.lang.Object arg0, java.lang.StringBuffer arg1, java.text.FieldPosition arg2, How to use format method of DateFormat?, Usage of DateFormat.format, DateFormat.format() examples
System.out.println("getInstance = " + DateFormat.getInstance().format(new Date()));
System.out.println("getDateInstance = " + DateFormat.getDateInstance().format(new Date()));
System.out.println("getDateInstance(DateFormat.SHORT) = "
+ DateFormat.getDateInstance(DateFormat.SHORT).format(new Date()));
System.out.println("getDateInstance(DateFormat.MEDIUM) = "
+ DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()));
System.out.println(
"getDateInstance(DateFormat.LONG) = " + DateFormat.getDateInstance(DateFormat.LONG).format(new Date()));
System.out.println(
"getDateInstance(DateFormat.FULL) = " + DateFormat.getDateInstance(DateFormat.FULL).format(new Date()));
Output: getInstance = 11/2/19, 11:33 PM getDateInstance = Nov 2, 2019 getDateInstance(DateFormat.SHORT) = 11/2/19 getDateInstance(DateFormat.MEDIUM) = Nov 2, 2019 getDateInstance(DateFormat.LONG) = November 2, 2019 getDateInstance(DateFormat.FULL) = Saturday, November 2, 2019
Tag: Example for format method of class java.text.DateFormat., DateFormat format function example with arguments java.util.Date arg0, How to use format method of DateFormat?, Usage of DateFormat.format, DateFormat.format() examples
// Format with normal date format
StringBuffer buffer = new StringBuffer("The date is ");
FieldPosition fieldPosition = new FieldPosition(DateFormat.YEAR_FIELD);
StringBuffer formattedDate = DateFormat.getInstance().format(new Date(), buffer, fieldPosition);
System.out.println("formattedDate = " + formattedDate);
System.out.println("fieldPosition = " + fieldPosition);
System.out.println("Year value using field position is = "
+ formattedDate.substring(fieldPosition.getBeginIndex(), fieldPosition.getEndIndex()));
// Format with FULL date format
StringBuffer buffer_1 = new StringBuffer("The date is ");
FieldPosition fieldPosition_1 = new FieldPosition(DateFormat.MONTH_FIELD);
StringBuffer formattedDate_1 = DateFormat.getDateInstance(DateFormat.FULL).format(new Date(), buffer_1,
fieldPosition_1);
System.out.println("\nformattedDate_1 = " + formattedDate_1);
System.out.println("fieldPosition_1 = " + fieldPosition_1);
System.out.println("Year value using field position is = "
+ formattedDate_1.substring(fieldPosition_1.getBeginIndex(), fieldPosition_1.getEndIndex()));
Output: formattedDate = The date is 11/2/19, 11:33 PM fieldPosition = java.text.FieldPosition[field=1,attribute=null,beginIndex=17,endIndex=19] Year value using field position is = 19 formattedDate_1 = The date is Saturday, November 2, 2019 fieldPosition_1 = java.text.FieldPosition[field=2,attribute=null,beginIndex=22,endIndex=30] Year value using field position is = November
Tag: Example for format method of class java.text.DateFormat., DateFormat format function example with arguments java.util.Date arg0, java.lang.StringBuffer arg1, java.text.FieldPosition arg2, How to use format method of DateFormat?, Usage of DateFormat.format, DateFormat.format() examples
// Format using Date object
System.out.println("getInstance = " + DateFormat.getInstance().format(new Date()));
System.out.println("getDateInstance = " + DateFormat.getDateInstance().format(new Date()));
System.out.println("getDateInstance(DateFormat.SHORT) = "
+ DateFormat.getDateInstance(DateFormat.SHORT).format(new Date()));
System.out.println("getDateInstance(DateFormat.MEDIUM) = "
+ DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()));
System.out.println(
"getDateInstance(DateFormat.LONG) = " + DateFormat.getDateInstance(DateFormat.LONG).format(new Date()));
System.out.println(
"getDateInstance(DateFormat.FULL) = " + DateFormat.getDateInstance(DateFormat.FULL).format(new Date()));
// Format using Long object.
Long timeMills = System.currentTimeMillis();
System.out.println("[Long] getInstance = " + DateFormat.getInstance().format(timeMills));
System.out.println("[Long] getDateInstance = " + DateFormat.getDateInstance().format(timeMills));
System.out.println("[Long] getDateInstance(DateFormat.SHORT) = "
+ DateFormat.getDateInstance(DateFormat.SHORT).format(timeMills));
System.out.println("[Long] getDateInstance(DateFormat.MEDIUM) = "
+ DateFormat.getDateInstance(DateFormat.MEDIUM).format(timeMills));
System.out.println("[Long] getDateInstance(DateFormat.LONG) = "
+ DateFormat.getDateInstance(DateFormat.LONG).format(timeMills));
System.out.println("[Long] getDateInstance(DateFormat.FULL) = "
+ DateFormat.getDateInstance(DateFormat.FULL).format(timeMills));
Output: getInstance = 11/2/19, 11:33 PM getDateInstance = Nov 2, 2019 getDateInstance(DateFormat.SHORT) = 11/2/19 getDateInstance(DateFormat.MEDIUM) = Nov 2, 2019 getDateInstance(DateFormat.LONG) = November 2, 2019 getDateInstance(DateFormat.FULL) = Saturday, November 2, 2019 [Long] getInstance = 11/2/19, 11:33 PM [Long] getDateInstance = Nov 2, 2019 [Long] getDateInstance(DateFormat.SHORT) = 11/2/19 [Long] getDateInstance(DateFormat.MEDIUM) = Nov 2, 2019 [Long] getDateInstance(DateFormat.LONG) = November 2, 2019 [Long] getDateInstance(DateFormat.FULL) = Saturday, November 2, 2019
Tag: Example for format method of class java.text.Format., Format format function example with arguments java.lang.Object arg0, How to use format method of Format?, Usage of Format.format, Format.format() examples
AttributedCharacterIterator attributedCharacterIterator = DateFormat.getInstance()
.formatToCharacterIterator(new Date());
for (char c = attributedCharacterIterator.first(); c != CharacterIterator.DONE; c = attributedCharacterIterator
.next()) {
System.out.print(c + "_");
}
Output: 1_1_/_2_/_1_9_,_ _1_1_:_3_3_ _P_M_
Tag: Example for formatToCharacterIterator method of class java.text.Format., Format formatToCharacterIterator function example with arguments java.lang.Object arg0, How to use formatToCharacterIterator method of Format?, Usage of Format.formatToCharacterIterator, Format.formatToCharacterIterator() examples
Locale[] availableLocales = DateFormat.getAvailableLocales();
System.out.println("availableLocales = " + Arrays.asList(availableLocales));
Output: availableLocales = [, nn, ar_JO, bg, kea, nds, zu, am_ET, fr_DZ, ti_ET, bo_CN, hsb, qu_EC, ta_SG, lv, en_NU, en_MS, zh_SG_#Hans, en_GG, en_JM, vo, kkj, sv_SE, sr_ME, es_BO, dz_BT, mer, sah, en_ZM, fr_ML, br, ha_NG, ar_SA, fa_AF, dsb_DE, sk, os_GE, ml, en_MT, en_LR, ar_TD, en_GH, en_IL, cs, sv, el, tzm_MA, af, sw_UG, ses_ML, smn, tk_TM, sr_ME_#Cyrl, ar_EG, dsb, lkt_US, vai_LR_#Latn, ji_001, yo_NG, se_NO, khq, sw_CD, vo_001, en_PW, pl_PL, fil_PH, it_VA, sr_CS, ne_IN, es_PH, es_ES, es_CO, bg_BG, ji, ar_EH, bs_BA_#Latn, en_VC, nds_DE, nb_SJ, es_US, agq, hsb_DE, en_US_POSIX, en_150, ar_SD, en_KN, ha_NE, pt_MO, ebu, ro_RO, zh__#Hans, lb_LU, sr_ME_#Latn, es_GT, so_KE, dje_NE, bas_CM, fr_PM, ar_KM, fr_MG, no_NO_NY, es_CL, mn, agq_CM, kam_KE, teo, tr_TR, eu, fa_IR, en_MO, wo, shi__#Tfng, en_BZ, sq_AL, ar_MR, es_DO, ru, twq_NE, az, nmg_CM, fa, kl_GL, en_NR, nd, kk, az__#Cyrl, en_MP, en_GD, tk, hy, shi__#Latn, en_BW, en_AU, en_CY, kab_DZ, kde_TZ, ta_MY, ti_ER, nus_SS, en_RW, nd_ZW, sv_FI, ksb, luo, lb, ne, en_IE, ln_CD, zh_SG, en_KI, nnh_CM, om_ET, no, ja_JP, my, ka, ar_IL, mgh, or_IN, fr_MF, shi, kl, en_SZ, rwk_TZ, zh, mgh_MZ, es_PE, saq, az__#Latn, ta, en_GB, lag, zh_HK_#Hant, ar_SY, ksf_CM, bo, kk_KZ, tt_RU, es_PA, om_KE, ar_PS, en_AS, fr_VU, bez, zh_TW, kln, fr_MC, kw, pt_MZ, fr_NE, vai__#Latn, ksb_TZ, ksh, ur_IN, ln, en_JE, gsw_CH, ln_CF, en_CX, luy_KE, pt, en_AT, gl, kkj_CM, sr__#Cyrl, yue_CN_#Hans, es_GQ, kn_IN, ar_YE, to, en_SX, ga, qu, ru_KZ, en_TZ, et, en_PR, mua, ko_KP, in, ps, sn, nl_SR, rof, en_BS, km, zgh, fr_NC, be, gv, es, dua, gd_GB, jgo, nl_BQ, fr_CM, gsw, uz_UZ_#Cyrl, pa_IN_#Guru, en_KE, guz, mfe, asa_TZ, teo_UG, ja, fr_SN, or, brx, fr_MA, pt_LU, fr_BL, en_NL, mgo_CM, ln_CG, te, ko_KR, mr_IN, ha, sl, el_CY, es_MX, lrc_IR, gsw_FR, es_HN, hu_HU, ff_SN, sbp, sq_MK, sr_BA_#Cyrl, fi, uz, bs__#Cyrl, et_EE, sr__#Latn, en_SS, sw, bo_IN, fy_NL, ar_OM, tr_CY, nmg, rm, en_MG, fr_BI, uz_UZ_#Latn, bn, dua_CM, de_IT, lrc_IQ, vai__#Vaii, kn, fr_TN, sr_RS, de_CH, bn_BD, nnh, fr_PF, gu, en_ZA, pt_GQ, vun_TZ, jmc_TZ, en_TV, lo, fr_FR, en_PN, en_MH, fr_BJ, zh__#Hant, cu_RU, zh_HK_#Hans, nl_NL, sah_RU, en_GY, ps_AF, bs__#Latn, ky, mas, dyo_SN, os, bs_BA_#Cyrl, nl_CW, ar_DZ, sk_SK, pt_CH, fr_GQ, ff_CM, am, en_NG, fr_CI, ki_KE, en_PK, zh_CN, en_LC, rw, brx_IN, wo_SN, iw, gv_IM, mk_MK, en_TT, dav, sl_SI, fr_HT, te_IN, nl_SX, lrc, ses, ce, fr_CG, fr_BE, jgo_CM, mt_MT, es_VE, mg, mr, mer_KE, ko, nds_NL, en_BM, nb_NO, ak, seh, kde, dz, kea_CV, mgo, vi_VN, en_VU, en_US, to_TO, mfe_MU, seh_MZ, fr_BF, pa__#Guru, it_SM, fr_YT, gu_IN, ii_CN, pa_PK_#Arab, ast, fr_RE, fi_FI, yue__#Hans, ca_FR, sr_BA_#Latn, bn_IN, fr_GP, pa, zgh_MA, fr_DJ, rn, tg, rwk, uk_UA, en_NF, fr_CH, hu, twq, ha_GH, sr_XK_#Cyrl, bm, ar_SS, en_GU, nl_AW, de_BE, en_AI, en_CM, xog_UG, cs_CZ, ca_ES, tr, cgg, rm_CH, nyn_UG, ru_MD, ms_MY, ta_LK, ksf, en_TO, cy, en_PG, fr_CF, pt_TL, fr, sq, tg_TJ, en_ER, qu_PE, sr_BA, es_PY, de, kok_IN, es_EC, lg_UG, zu_ZA, fr_TG, sr_XK_#Latn, en_PH, ig_NG, fr_GN, prg_001, cgg_UG, zh_MO_#Hans, ksh_DE, lg, ru_RU, se_FI, ff, en_DM, en_CK, sd, ar_MA, en_BI, ga_IE, en_AG, fr_TD, en_WS, fr_LU, ebu_KE, bem_ZM, xog, ewo_CM, fr_CD, so, rn_BI, en_NA, ar_ER, kab, ms, nus, sn_ZW, prg, iw_IL, ug, es_EA, th_TH_TH_#u-nu-thai, hi, fr_SC, ca_IT, lag_TZ, en_SL, teo_KE, ca_AD, no_NO, zh_MO_#Hant, en_SH, vai, qu_BO, haw_US, vi, fr_CA, sq_XK, dyo, de_LU, en_KY, mt, it_CH, de_DE, si_LK, luo_KE, en_DK, yav, so_DJ, eo, it_IT, lt_LT, kam, ar_SO, en_ZW, ro, eo_001, ee, en_UM, nn_NO, fr_MU, se_SE, pl, en_TK, en_SI, mua_CM, ur, uz__#Arab, vai_LR_#Vaii, saq_KE, se, pt_GW, lo_LA, chr, af_ZA, ar_LB, ms_SG, ee_TG, ln_AO, ff_GN, be_BY, yue__#Hant, in_ID, es_BZ, ar_AE, hr_HR, luy, as, rof_TZ, it, pt_CV, ks_IN, uk, my_MM, ur_PK, mn_MN, en_FM, da_DK, es_PR, wae_CH, mzn, en_BE, ii, tt, fr_WF, ru_BY, mzn_IR, naq, fo_DK, en_SG, ee_GH, ar_BH, kln_KE, tzm, fur, om, hi_IN, en_CH, asa, yo_BJ, fo_FO, ast_ES, fr_KM, bez_TZ, fr_MQ, en_SD, es_AR, en_MY, ja_JP_JP_#u-ca-japanese, es_SV, pt_BR, ml_IN, sbp_TZ, fil, en_FK, uz__#Cyrl, is_IS, yue_HK_#Hant, hy_AM, en_GM, en_DG, fo, ne_NP, pt_ST, hr, ak_GH, lt, uz_AF_#Arab, fur_IT, ta_IN, ccp, en_SE, fr_GF, lkt, zh_CN_#Hans, is, es_419, si, pt_AO, en_001, en, guz_KE, gsw_LI, ccp_BD, es_IC, ca, ru_KG, fr_MR, ar_TN, ks, zh_TW_#Hant, bm_ML, kw_GB, ug_CN, as_IN, es_BR, zh_HK, khq_ML, sw_KE, en_SB, rw_RW, chr_US, th_TH, shi_MA_#Tfng, ar_IQ, nyn, yue, jmc, en_MW, naq_NA, mk, en_IO, ar_QA, en_DE, pa__#Arab, en_CC, bs, ro_MD, en_FI, pt_PT, fy, az_AZ_#Cyrl, th, dav_KE, ckb_IQ, shi_MA_#Latn, es_CU, ar, en_SC, en_VI, haw, eu_ES, en_UG, dje, en_NZ, bas, es_UY, mas_KE, ru_UA, sg_CF, yav_CM, uz__#Latn, el_GR, sg, da_GL, en_FJ, de_LI, en_BB, km_KH, smn_FI, hr_BA, de_AT, ckb_IR, nl, lu_CD, ca_ES_VALENCIA, ar_001, so_SO, lv_LV, ckb, es_CR, fr_GA, ar_KW, ar_LY, sr, sr_RS_#Cyrl, bem, en_MU, da, wae, gl_ES, en_IM, az_AZ_#Latn, en_LS, ig, en_HK, en_GI, ce_RU, gd, en_CA, ka_GE, fr_SY, sw_TZ, fr_RW, so_ET, nl_BE, ar_DJ, mg_MG, cy_GB, en_VG, cu, os_RU, sr_RS_#Latn, en_TC, ky_KG, sv_AX, af_NA, vun, en_IN, lu, ki, yo, es_NI, nb, ff_MR, sd_PK, mas_TZ, ti, kok, ewo, ms_BN, ccp_IN, br_FR]
Tag: Example for getAvailableLocales method of class java.text.DateFormat., DateFormat getAvailableLocales function example , How to use getAvailableLocales method of DateFormat?, Usage of DateFormat.getAvailableLocales, DateFormat.getAvailableLocales() examples
Calendar calendar = DateFormat.getDateInstance(DateFormat.FULL).getCalendar();
calendar.setTime(new Date());
System.out.println(calendar);
Output: java.util.GregorianCalendar[time=1572762833943,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=10,WEEK_OF_YEAR=44,WEEK_OF_MONTH=1,DAY_OF_MONTH=2,DAY_OF_YEAR=306,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=33,SECOND=53,MILLISECOND=943,ZONE_OFFSET=-28800000,DST_OFFSET=3600000]
Tag: Example for getCalendar method of class java.text.DateFormat., DateFormat getCalendar function example , How to use getCalendar method of DateFormat?, Usage of DateFormat.getCalendar, DateFormat.getCalendar() examples
Import statements used in examples.
java.text.AttributedCharacterIterator
java.text.CharacterIterator
java.text.DateFormat
java.text.FieldPosition
java.text.NumberFormat
java.text.ParsePosition
java.util.Arrays
java.util.Calendar
java.util.Date
java.util.Locale
java.util.TimeZone
Tag: Simple working examples of methods / functions of class java.text.DateFormat along with their console output, java.text.DateFormat tutorial., Guide to java.text.DateFormat & its methods., Understanding java.text.DateFormat with examples.