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

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

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

DateFormat.getDateInstance介绍

[英]Returns a DateFormat instance for formatting and parsing dates in the DEFAULT style for the default locale.
[中]返回一个DateFormat实例,用于以默认语言环境的默认样式格式化和分析日期。

代码示例

代码示例来源:origin: SonarSource/sonarqube

@Override
public String formatDate(Locale locale, Date date) {
 return DateFormat.getDateInstance(DateFormat.DEFAULT, locale).format(date);
}

代码示例来源:origin: prestodb/presto

String getPattern(Locale locale) {
    DateFormat f = null;
    switch (iType) {
      case DATE:
        f = DateFormat.getDateInstance(iDateStyle, locale);
        break;
      case TIME:
        f = DateFormat.getTimeInstance(iTimeStyle, locale);
        break;
      case DATETIME:
        f = DateFormat.getDateTimeInstance(iDateStyle, iTimeStyle, locale);
        break;
    }
    if (f instanceof SimpleDateFormat == false) {
      throw new IllegalArgumentException("No datetime pattern for locale: " + locale);
    }
    return ((SimpleDateFormat) f).toPattern();
  }
}

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

Locale locale = Locale.getDefault();
SimpleDateFormat sdf = (SimpleDateFormat)DateFormat.getDateInstance(DateFormat.SHORT, locale );
String format = sdf.toPattern();

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

String currentDateTimeString = DateFormat.getDateInstance().format(new Date());

// textView is the TextView view that should display it
textView.setText(currentDateTimeString);

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

DateFormat.getDateInstance().format(new Date(0));  
 DateFormat.getDateTimeInstance().format(new Date(0));  
 DateFormat.getTimeInstance().format(new Date(0));

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

public static void main(String[] args) throws Exception {
  for (Locale locale : Locale.getAvailableLocales()) {
    DateFormat df = getShortDateInstanceWithoutYears(locale);
    System.out.println(locale + ": " + df.format(new Date()));      
  }
}

public static DateFormat getShortDateInstanceWithoutYears(Locale locale) {
  SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale);
  sdf.applyPattern(sdf.toPattern().replaceAll("[^\\p{Alpha}]*y+[^\\p{Alpha}]*", ""));
  return sdf;
}

代码示例来源:origin: aws-amplify/aws-sdk-android

@Override
@SuppressWarnings("checkstyle:emptyblock")
public Date deserialize(JsonElement element, Type arg1, JsonDeserializationContext context) {
  final String dateString = element.getAsString();
  Date date = null;
  for (final String df : dateFormats) {
    try {
      date = new Date();
      mSimpleDateFormat = new SimpleDateFormat(df);
      date.setTime(mSimpleDateFormat.parse(dateString).getTime());
      return date;
    } catch (final ParseException e) {
      // swallow , will try next type of date format
    }
  }
  // default to default implementation
  try {
    return DateFormat.getDateInstance(DateFormat.DEFAULT).parse(dateString);
  } catch (final ParseException e) {
    throw new JsonParseException(e.getMessage(), e);
  }
}

代码示例来源:origin: spring-projects/spring-framework

private DateFormat createDateFormat(Locale locale) {
  if (StringUtils.hasLength(this.pattern)) {
    return new SimpleDateFormat(this.pattern, locale);
  }
  if (this.iso != null && this.iso != ISO.NONE) {
    String pattern = ISO_PATTERNS.get(this.iso);
    if (pattern == null) {
      throw new IllegalStateException("Unsupported ISO format " + this.iso);
    }
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    format.setTimeZone(UTC);
    return format;
  }
  if (StringUtils.hasLength(this.stylePattern)) {
    int dateStyle = getStylePatternForChar(0);
    int timeStyle = getStylePatternForChar(1);
    if (dateStyle != -1 && timeStyle != -1) {
      return DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
    }
    if (dateStyle != -1) {
      return DateFormat.getDateInstance(dateStyle, locale);
    }
    if (timeStyle != -1) {
      return DateFormat.getTimeInstance(timeStyle, locale);
    }
    throw new IllegalStateException("Unsupported style pattern '" + this.stylePattern + "'");
  }
  return DateFormat.getDateInstance(this.style, locale);
}

代码示例来源:origin: pentaho/mondrian

java.text.DateFormat.getDateInstance(
    java.text.DateFormat.SHORT, locale);
final String dateValue = dateFormat.format(date); // "12/31/69"
String dateSeparator = dateValue.substring(2, 3); // "/"
  java.text.DateFormat.getTimeInstance(
    java.text.DateFormat.SHORT, locale);
final String timeValue = timeFormat.format(date); // "12:00:00"
String timeSeparator = timeValue.substring(2, 3); // ":"

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

private String decodeSimpleDateFormat(StringBuffer buffer, Format format) {
  if (format.equals(DateFormat.getTimeInstance(DateFormat.DEFAULT, locale))) {
    buffer.append(",time");
  } else if (format.equals(DateFormat.getDateInstance(DateFormat.DEFAULT,
      locale))) {
    buffer.append(",date");
  } else if (format.equals(DateFormat.getTimeInstance(DateFormat.SHORT,
      locale))) {
    buffer.append(",time,short");
  } else if (format.equals(DateFormat.getDateInstance(DateFormat.SHORT,
      locale))) {
    buffer.append(",date,short");
  } else if (format.equals(DateFormat.getTimeInstance(DateFormat.LONG,
      locale))) {
    buffer.append(",time,long");
  } else if (format.equals(DateFormat.getDateInstance(DateFormat.LONG,
      locale))) {
    buffer.append(",date,long");
  } else if (format.equals(DateFormat.getTimeInstance(DateFormat.FULL,
      locale))) {
    buffer.append(",time,full");
  } else if (format.equals(DateFormat.getDateInstance(DateFormat.FULL,
      locale))) {
    buffer.append(",date,full");
  } else {
    buffer.append(",date,");
    return ((SimpleDateFormat) format).toPattern();
  }
  return null;
}

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

case 1: // date
  if (ch == '}') {
    return type == 1 ? DateFormat.getDateInstance(
        DateFormat.DEFAULT, locale) : DateFormat
        .getTimeInstance(DateFormat.DEFAULT, locale);
      .getDateInstance(dateStyle, locale) : DateFormat
      .getTimeInstance(dateStyle, locale);
case 2: // number
  if (ch == '}') {

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

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class DateFormatDemoSO {
 public static void main(String args[]) {
  int style = DateFormat.MEDIUM;
  //Also try with style = DateFormat.FULL and DateFormat.SHORT
  Date date = new Date();
  DateFormat df;
  df = DateFormat.getDateInstance(style, Locale.UK);
  System.out.println("United Kingdom: " + df.format(date));
  df = DateFormat.getDateInstance(style, Locale.US);
  System.out.println("USA: " + df.format(date));   
  df = DateFormat.getDateInstance(style, Locale.FRANCE);
  System.out.println("France: " + df.format(date));
  df = DateFormat.getDateInstance(style, Locale.ITALY);
  System.out.println("Italy: " + df.format(date));
  df = DateFormat.getDateInstance(style, Locale.JAPAN);
  System.out.println("Japan: " + df.format(date));
 }
}

代码示例来源:origin: openmrs/openmrs-core

dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
} else if (type == FORMAT_TYPE.TIME) {
  dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
} else {
  dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
return dateFormat.format(date);

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

locales.add(Locale.CHINA);
Date date = new Date();
  SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateInstance(DateFormat.SHORT, l);
  String pattern = sdf.toPattern();
  String localizedPattern = sdf.toLocalizedPattern()
  println "country: " + l.getDisplayName();
  String dateString = df.format(date);
  println "resulting date: " + dateString
  String yearlessPattern = pattern.replaceAll("\\W?[Yy]+\\W?", "");
  println "yearlessPattern = " + yearlessPattern;
  SimpleDateFormat yearlessSDF = new SimpleDateFormat(yearlessPattern, l);
  println "resulting date without year: " + yearlessSDF.format(date) + "\n";

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

@AfterViews
void updateCaption() {
  DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.US);
  String formattedDate = dateFormat.format(date);
  injectedTextView.setText(caption + " - " + formattedDate);
}

代码示例来源:origin: joda-time/joda-time

String getPattern(Locale locale) {
    DateFormat f = null;
    switch (iType) {
      case DATE:
        f = DateFormat.getDateInstance(iDateStyle, locale);
        break;
      case TIME:
        f = DateFormat.getTimeInstance(iTimeStyle, locale);
        break;
      case DATETIME:
        f = DateFormat.getDateTimeInstance(iDateStyle, iTimeStyle, locale);
        break;
    }
    if (f instanceof SimpleDateFormat == false) {
      throw new IllegalArgumentException("No datetime pattern for locale: " + locale);
    }
    return ((SimpleDateFormat) f).toPattern();
  }
}

代码示例来源:origin: org.springframework/spring-context

private DateFormat createDateFormat(Locale locale) {
  if (StringUtils.hasLength(this.pattern)) {
    return new SimpleDateFormat(this.pattern, locale);
  }
  if (this.iso != null && this.iso != ISO.NONE) {
    String pattern = ISO_PATTERNS.get(this.iso);
    if (pattern == null) {
      throw new IllegalStateException("Unsupported ISO format " + this.iso);
    }
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    format.setTimeZone(UTC);
    return format;
  }
  if (StringUtils.hasLength(this.stylePattern)) {
    int dateStyle = getStylePatternForChar(0);
    int timeStyle = getStylePatternForChar(1);
    if (dateStyle != -1 && timeStyle != -1) {
      return DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
    }
    if (dateStyle != -1) {
      return DateFormat.getDateInstance(dateStyle, locale);
    }
    if (timeStyle != -1) {
      return DateFormat.getTimeInstance(timeStyle, locale);
    }
    throw new IllegalStateException("Unsupported style pattern '" + this.stylePattern + "'");
  }
  return DateFormat.getDateInstance(this.style, locale);
}

代码示例来源:origin: org.apache.poi/poi

public static String getJavaDatePattern(int style, Locale locale) {
  DateFormat df = DateFormat.getDateInstance(style, locale);
  if( df instanceof SimpleDateFormat ) {
    return ((SimpleDateFormat)df).toPattern();
  } else {
    switch( style ) {
    case DateFormat.SHORT:
      return "d/MM/yy";
    case DateFormat.MEDIUM:
      return "MMM d, yyyy";
    case DateFormat.LONG:
      return "MMMM d, yyyy";
    case DateFormat.FULL:
      return "dddd, MMMM d, yyyy";
    default:
      return "MMM d, yyyy";
    }
  }
}

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

DateFormat df = null;
if (type.equals("both")) {
  df = DateFormat.getDateInstance(getStyle(dateStyle), this.getLocale());
  metadata.put(HTML.VALIDATION_METADATA.DATE_STYLE_PATTERN, CalendarUtils.convertPattern(((SimpleDateFormat) df).toPattern()));
  df = DateFormat.getTimeInstance(getStyle(timeStyle), this.getLocale());
  metadata.put(HTML.VALIDATION_METADATA.TIME_STYLE_PATTERN, CalendarUtils.convertPattern(((SimpleDateFormat) df).toPattern()));
  df = DateFormat.getDateInstance(getStyle(dateStyle), this.getLocale());
  metadata.put(HTML.VALIDATION_METADATA.DATE_STYLE_PATTERN, CalendarUtils.convertPattern(((SimpleDateFormat) df).toPattern()));
  df = DateFormat.getTimeInstance(getStyle(timeStyle), this.getLocale());
  metadata.put(HTML.VALIDATION_METADATA.TIME_STYLE_PATTERN, CalendarUtils.convertPattern(((SimpleDateFormat) df).toPattern()));

代码示例来源: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()));
}

相关文章