Java Code Examples for java.text.DateFormat.format()

The following are Jave code examples for showing how to use format() of the java.text.DateFormat class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: xlight_android_native   File: DateUtil.java   View Source Code Vote up 5 votes
public static String getPreviousYearEnd() {
    weeks--;
    int yearPlus = getYearPlus();
    GregorianCalendar currentDate = new GregorianCalendar();
    currentDate.add(GregorianCalendar.DATE, yearPlus + MaxYear * weeks
            + (MaxYear - 1));
    Date yearDay = currentDate.getTime();
    DateFormat df = DateFormat.getDateInstance();
    String preYearDay = df.format(yearDay);
    getThisSeasonTime(11);
    return preYearDay;
}
 
Example 2
Project: L2J-Global   File: Util.java   View Source Code Vote up 5 votes
/**
 * Format the given date on the given format
 * @param date : the date to format.
 * @param format : the format to correct by.
 * @return a string representation of the formatted date.
 */
public static String formatDate(Date date, String format)
{
	if (date == null)
	{
		return null;
	}
	final DateFormat dateFormat = new SimpleDateFormat(format);
	return dateFormat.format(date);
}
 
Example 3
Project: parabuild-ci   File: DateAxis.java   View Source Code Vote up 5 votes
/**
 * Estimates the maximum width of the tick labels, assuming the specified tick unit is used.
 * <P>
 * Rather than computing the string bounds of every tick on the axis, we just look at two
 * values: the lower bound and the upper bound for the axis.  These two values will usually
 * be representative.
 *
 * @param g2  the graphics device.
 * @param unit  the tick unit to use for calculation.
 *
 * @return the estimated maximum width of the tick labels.
 */
private double estimateMaximumTickLabelHeight(Graphics2D g2, DateTickUnit unit) {

    Insets tickLabelInsets = getTickLabelInsets();
    double result = tickLabelInsets.top + tickLabelInsets.bottom;

    Font tickLabelFont = getTickLabelFont();
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics lm = tickLabelFont.getLineMetrics("ABCxyz", frc);
    if (!isVerticalTickLabels()) {
        // all tick labels have the same width (equal to the height of the font)...
        result += lm.getHeight();
    }
    else {
        // look at lower and upper bounds...
        DateRange range = (DateRange) getRange();
        Date lower = range.getLowerDate();
        Date upper = range.getUpperDate();
        String lowerStr = null;
        String upperStr = null;
        DateFormat formatter = getDateFormatOverride();
        if (formatter != null) {
            lowerStr = formatter.format(lower);
            upperStr = formatter.format(upper);
        }
        else {
            lowerStr = unit.dateToString(lower);
            upperStr = unit.dateToString(upper);
        }
        FontMetrics fm = g2.getFontMetrics(tickLabelFont);
        double w1 = fm.stringWidth(lowerStr);
        double w2 = fm.stringWidth(upperStr);
        result += Math.max(w1, w2);
    }

    return result;

}
 
Example 4
Project: Hotspot-master-devp   File: TimeUtils.java   View Source Code Vote up 5 votes
/**
 * 格式化时间
 *
 * @param time
 * @return
 */
@SuppressLint("SimpleDateFormat")
public static String formatPlayTime(long time) {
    if(time<=0)
        return "00:00";
    DateFormat formatter = new SimpleDateFormat("mm:ss");
    return formatter.format(new Date(time));
}
 
Example 5
Project: jdk8u-jdk   File: TCKLocalizedPrinterParser.java   View Source Code Vote up 5 votes
@SuppressWarnings("deprecation")
@Test(dataProvider="date")
public void test_date_print(LocalDate date, FormatStyle dateStyle, int dateStyleOld, Locale locale) {
    DateFormat old = DateFormat.getDateInstance(dateStyleOld, locale);
    Date oldDate = new Date(date.getYear() - 1900, date.getMonthValue() - 1, date.getDayOfMonth());
    String text = old.format(oldDate);

    DateTimeFormatter f = builder.appendLocalized(dateStyle, null).toFormatter(locale);
    String formatted = f.format(date);
    assertEquals(formatted, text);
}
 
Example 6
Project: lams   File: JspHelper.java   View Source Code Vote up 5 votes
public static String getDisplayCreationTimeForSession(Session in_session) {
      try {
	DateFormat formatter = new SimpleDateFormat(DATE_TIME_FORMAT);
	return formatter.format(new Date(in_session.getCreationTime()));
      } catch (IllegalStateException ise) {
      	//ignore: invalidated session
      	return "";
}
  }
 
Example 7
Project: parabuild-ci   File: BubbleXYItemLabelGenerator.java   View Source Code Vote up 5 votes
/**
 * Creates the array of items that can be passed to the 
 * {@link MessageFormat} class for creating labels.
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param series  the series (zero-based index).
 * @param item  the item (zero-based index).
 *
 * @return The items (never <code>null</code>).
 */
protected Object[] createItemArray(XYZDataset dataset, 
                                   int series, int item) {

    Object[] result = new Object[4];
    result[0] = dataset.getSeriesKey(series).toString();
 
    Number x = dataset.getX(series, item);
    DateFormat xf = getXDateFormat();
    if (xf != null) {
        result[1] = xf.format(x);   
    }
    else {
        result[1] = getXFormat().format(x);
    }
    
    Number y = dataset.getY(series, item);
    DateFormat yf = getYDateFormat();
    if (yf != null) {
        result[2] = yf.format(y);
    }
    else {
        result[2] = getYFormat().format(y);
    }
    
    Number z = dataset.getZ(series, item);
    if (this.zDateFormat != null) {
        result[3] = this.zDateFormat.format(z);   
    }
    else {
        result[3] = this.zFormat.format(z);   
    }
    
    return result;
    
}
 
Example 8
Project: FreeCol   File: HighScore.java   View Source Code Vote up 4 votes
/**
 * Get the {@code Date} the score was achieved as a formatted string.
 *
 * @return The date string.
 */
public final String getDateString() {
    DateFormat format = DateFormat
        .getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
    return format.format(date);
}
 
Example 9
Project: tangyuan2   File: DateUtils.java   View Source Code Vote up 4 votes
public static String getDateString(java.sql.Date date, String pattern) {
	DateFormat fmt = new SimpleDateFormat(pattern);
	return fmt.format(date);
}
 
Example 10
Project: otus_java_2017_06   File: TimeService.java   View Source Code Vote up 4 votes
public String getTime() {
    Date date = new Date();
    date.getTime();
    DateFormat formatter = new SimpleDateFormat(pattern);
    return formatter.format(date);
}
 
Example 11
Project: SOS-The-Healthcare-Companion   File: SplitDateTime.java   View Source Code Vote up 4 votes
public String getMonth() {
    DateFormat finalFormat = new SimpleDateFormat("MM");
    return finalFormat.format(origDateTime);
}
 
Example 12
Project: okwallet   File: Utils.java   View Source Code Vote up 4 votes
/**
 * Formats a given date+time value to an ISO 8601 string.
 * @param dateTime value to format, as a Date
 */
public static String dateTimeFormat(Date dateTime) {
    DateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
    iso8601.setTimeZone(UTC);
    return iso8601.format(dateTime);
}
 
Example 13
Project: otus_java_2017_04   File: TimerServlet.java   View Source Code Vote up 4 votes
private static String getTime() {
    Date date = new Date();
    date.getTime();
    DateFormat formatter = new SimpleDateFormat("HH.mm.ss");
    return formatter.format(date);
}
 
Example 14
Project: Reinickendorf_SER316   File: Local.java   View Source Code Vote up 4 votes
public static String getTimeString(Date d) {
    DateFormat dateFormat =
        DateFormat.getTimeInstance(DateFormat.SHORT, currentLocale);
    return dateFormat.format(d);
}
 
Example 15
Project: BuddyBook   File: DateUtils.java   View Source Code Vote up 4 votes
public static String formatStringDate(String date){

        String result = "";

        if (date == null) return result;

        DateFormat fmtOut = new SimpleDateFormat(MASK_PUBLISH_OUT, Locale.US);
        DateFormat fmtIn = new SimpleDateFormat(MASK_PUBLISH_IN, Locale.US);


        try {

            Date dataParsed = fmtIn.parse(date);

            result = fmtOut.format(dataParsed);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return result;
    }
 
Example 16
Project: sirocco   File: DateTZ.java   View Source Code Vote up 4 votes
public String toString()
{
	DateFormat outdfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssz");
	outdfm.setTimeZone(tz);
	return outdfm.format(this);
}
 
Example 17
Project: AndroidSnooper   File: HttpCallViewModel.java   View Source Code Vote up 4 votes
public String getTimeStamp() {
  DateFormat df = new SimpleDateFormat(TIMESTAMP_FORMAT, US);
  return df.format(httpCall.getDate());
}
 
Example 18
Project: UiLib   File: MediaController.java   View Source Code Vote up 4 votes
@SuppressLint("SimpleDateFormat")
private String formatPlayTime(long time) {
    DateFormat formatter = new SimpleDateFormat("mm:ss");
    return formatter.format(new Date(time));
}
 
Example 19
Project: Reinickendorf_SER316   File: Local.java   View Source Code Vote up 4 votes
public static String getDateString(Date d, int f) {
    DateFormat dateFormat = DateFormat.getDateInstance(f, currentLocale);
    return dateFormat.format(d);
}
 
Example 20
Project: Android-UtilCode   File: TimeUtils.java   View Source Code Vote up 2 votes
/**
 * 将时间戳转为时间字符串
 * <p>格式为format</p>
 *
 * @param millis 毫秒时间戳
 * @param format 时间格式
 * @return 时间字符串
 */
public static String millis2String(long millis, DateFormat format) {
    return format.format(new Date(millis));
}