java.text.DateFormat.format()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(276)

本文整理了Java中java.text.DateFormat.format()方法的一些代码示例,展示了DateFormat.format()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。DateFormat.format()方法的具体详情如下:
包路径:java.text.DateFormat
类名称:DateFormat
方法名:format

DateFormat.format介绍

[英]Formats the specified object as a string using the pattern of this date format and appends the string to the specified string buffer.

If the field member of field contains a value specifying a format field, then its beginIndex and endIndex members will be updated with the position of the first occurrence of this field in the formatted text.
[中]使用此日期格式的模式将指定对象格式化为字符串,并将该字符串附加到指定的字符串缓冲区。
如果字段的字段成员包含指定格式字段的值,则其beginIndex和endIndex成员将更新为该字段在格式化文本中第一次出现的位置。

代码示例

代码示例来源:origin: stackoverflow.com

// Create an instance of SimpleDateFormat used for formatting 
// the string representation of date (month/day/year)
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

// Get the date today using Calendar object.
Date today = Calendar.getInstance().getTime();        
// Using DateFormat format method we can create a string 
// representation of a date with the defined format.
String reportDate = df.format(today);

// Print what date is today!
System.out.println("Report Date: " + reportDate);

代码示例来源:origin: redisson/redisson

private String getFormattedDate() {
  Date now = new Date();
  String dateText;
  synchronized (CONFIG_PARAMS.dateFormatter) {
    dateText = CONFIG_PARAMS.dateFormatter.format(now);
  }
  return dateText;
}

代码示例来源:origin: stackoverflow.com

String newstring = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println(newstring); // 2011-01-18

代码示例来源:origin: stackoverflow.com

Date now = new Date(); // java.util.Date, NOT java.sql.Date or java.sql.Timestamp!
String format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH).format(now);
String format2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH).format(now);
String format3 = new SimpleDateFormat("yyyyMMddHHmmss", Locale.ENGLISH).format(now);

System.out.println(format1);
System.out.println(format2);
System.out.println(format3);

代码示例来源:origin: jenkinsci/jenkins

private static String clientDateString() {
  TimeZone tz = TimeZone.getTimeZone("UTC");
  DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
  df.setTimeZone(tz); // strip timezone
  return df.format(new Date());
}

代码示例来源:origin: stackoverflow.com

Calendar cal = new GregorianCalendar();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
dateFormat.setTimeZone(cal.getTimeZone());
System.out.println(dateFormat.format(cal.getTime()));

代码示例来源:origin: stanfordnlp/CoreNLP

public void initLog(File logFilePath) throws IOException {
 RedwoodConfiguration.empty()
  .handlers(RedwoodConfiguration.Handlers.chain(
   RedwoodConfiguration.Handlers.showAllChannels(), RedwoodConfiguration.Handlers.stderr),
   RedwoodConfiguration.Handlers.file(logFilePath.toString())
  ).apply();
 // fh.setFormatter(new NewlineLogFormatter());
 System.out.println("Starting Ssurgeon log, at "+logFilePath.getAbsolutePath()+" date=" + DateFormat.getDateInstance(DateFormat.FULL).format(new Date()));
 log.info("Starting Ssurgeon log, date=" + DateFormat.getDateInstance(DateFormat.FULL).format(new Date()));
}

代码示例来源:origin: stackoverflow.com

String dateStr = "04/05/2010"; 

SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); 
Date dateObj = curFormater.parse(dateStr); 
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy"); 

String newDateStr = postFormater.format(dateObj);

代码示例来源:origin: robolectric/robolectric

@Test
public void getTimeFormat_returnsATimeFormat() {
 Calendar cal = Calendar.getInstance();
 cal.clear();
 cal.set(Calendar.HOUR, 7);
 cal.set(Calendar.MINUTE, 48);
 cal.set(Calendar.SECOND, 3);
 Date date = cal.getTime();
 assertThat(getTimeFormat(context).format(date)).isEqualTo("07:48:03");
}

代码示例来源:origin: alibaba/fastjson

public void write(JSONSerializer serializer, Object object, BeanContext context) throws IOException {
  SerializeWriter out = serializer.out;
  String format = context.getFormat();
  Calendar calendar = (Calendar) object;
  if (format.equals("unixtime")) {
    long seconds = calendar.getTimeInMillis() / 1000L;
    out.writeInt((int) seconds);
    return;
  }
  DateFormat dateFormat = new SimpleDateFormat(format);
  if (dateFormat == null) {
    dateFormat = new SimpleDateFormat(JSON.DEFFAULT_DATE_FORMAT, serializer.locale);
    dateFormat.setTimeZone(serializer.timeZone);
  }
  String text = dateFormat.format(calendar.getTime());
  out.writeString(text);
}

代码示例来源:origin: stackoverflow.com

public class Foo
{
  // SimpleDateFormat is not thread-safe, so give one to each thread
  private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>(){
    @Override
    protected SimpleDateFormat initialValue()
    {
      return new SimpleDateFormat("yyyyMMdd HHmm");
    }
  };

  public String formatIt(Date date)
  {
    return formatter.get().format(date);
  }
}

代码示例来源:origin: jenkinsci/jenkins

private void updateValidationsForNextRun(Collection<FormValidation> validations, CronTabList ctl) {
    try {
      Calendar prev = ctl.previous();
      Calendar next = ctl.next();
      if (prev != null && next != null) {
        DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
        validations.add(FormValidation.ok(Messages.TimerTrigger_would_last_have_run_at_would_next_run_at(fmt.format(prev.getTime()), fmt.format(next.getTime()))));
      } else {
        validations.add(FormValidation.warning(Messages.TimerTrigger_no_schedules_so_will_never_run()));
      }
    } catch (RareOrImpossibleDateException ex) {
      validations.add(FormValidation.warning(Messages.TimerTrigger_the_specified_cron_tab_is_rare_or_impossible()));
    }
  }
}

代码示例来源:origin: stackoverflow.com

// Format for input
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
// Parsing the date
Date date = dateParser.parse(dateTime);
// Format for output
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
// Printing the date
System.out.println(dateFormatter.format(date));

代码示例来源:origin: stackoverflow.com

String dt = "2008-01-01";  // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1);  // number of days to add
dt = sdf.format(c.getTime());  // dt is now the new date

代码示例来源:origin: stackoverflow.com

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(new Date());

代码示例来源:origin: androidannotations/androidannotations

protected String getTime() {
  return DATE_FORMAT.format(new Date());
}

代码示例来源:origin: stackoverflow.com

String input = "Thu Jun 18 20:56:02 EDT 2009";
   SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
   Date date = parser.parse(input);
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   String formattedDate = formatter.format(date);
   ...

代码示例来源:origin: robolectric/robolectric

@Test
public void getDateFormat_returnsADateFormat_December() {
 Calendar cal = Calendar.getInstance();
 cal.clear();
 cal.set(Calendar.DATE, 31);
 cal.set(Calendar.MONTH, Calendar.DECEMBER);
 cal.set(Calendar.YEAR, 1970);
 Date date = cal.getTime();
 assertThat(getDateFormat(context).format(date)).isEqualTo("Dec-31-1970");
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Returns the build time stamp in the body.
 */
public void doBuildTimestamp( StaplerRequest req, StaplerResponse rsp, @QueryParameter String format) throws IOException {
  rsp.setContentType("text/plain");
  rsp.setCharacterEncoding("US-ASCII");
  rsp.setStatus(HttpServletResponse.SC_OK);
  DateFormat df = format==null ?
      DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT, Locale.ENGLISH) :
      new SimpleDateFormat(format,req.getLocale());
  rsp.getWriter().print(df.format(getTime()));
}

代码示例来源:origin: org.testng/testng

@Override
public String getMessage() {
 if(isSkip()) {
  return super.getMessage();
 }
 else {
  return super.getMessage() + "; Test must have been enabled by: " + m_outFormat.format(m_expireDate.getTime());
 }
}

相关文章