java.text.DateFormat类的使用及代码示例

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

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

DateFormat介绍

[英]Formats or parses dates and times.

This class provides factories for obtaining instances configured for a specific locale. The most common subclass is SimpleDateFormat.

Sample Code

This code:

DateFormat[] formats = new DateFormat[] { 
DateFormat.getDateInstance(), 
DateFormat.getDateTimeInstance(), 
DateFormat.getTimeInstance(), 
}; 
for (DateFormat df : formats) { 
System.out.println(df.format(new Date(0))); 
df.setTimeZone(TimeZone.getTimeZone("UTC")); 
System.out.println(df.format(new Date(0))); 
}

Produces this output when run on an en_US device in the America/Los_Angeles time zone:

Dec 31, 1969 
Jan 1, 1970 
Dec 31, 1969 4:00:00 PM 
Jan 1, 1970 12:00:00 AM 
4:00:00 PM 
12:00:00 AM

And will produce similarly appropriate localized human-readable output on any user's system. Notice how the same point in time when formatted can appear to be a different time when rendered for a different time zone. This is one reason why formatting should be left until the data will only be presented to a human. Machines should interchange "Unix time" integers.
[中]格式化或解析日期和时间。
此类提供用于获取为特定区域设置配置的实例的工厂。最常见的子类是SimpleDataFormat。
#####示例代码
此代码:

DateFormat[] formats = new DateFormat[] { 
DateFormat.getDateInstance(), 
DateFormat.getDateTimeInstance(), 
DateFormat.getTimeInstance(), 
}; 
for (DateFormat df : formats) { 
System.out.println(df.format(new Date(0))); 
df.setTimeZone(TimeZone.getTimeZone("UTC")); 
System.out.println(df.format(new Date(0))); 
}

在美国/洛杉矶时区的en_US设备上运行时生成此输出:

Dec 31, 1969 
Jan 1, 1970 
Dec 31, 1969 4:00:00 PM 
Jan 1, 1970 12:00:00 AM 
4:00:00 PM 
12:00:00 AM

并将在任何用户的系统上生成类似的适当本地化的人类可读输出。请注意,格式化时的同一时间点在为不同时区渲染时可能看起来是不同的时间。这就是为什么在数据只呈现给用户之前,应该保留格式设置的原因之一。计算机应交换“Unix时间”整数。

代码示例

canonical example by Tabnine

public boolean isDateExpired(String input, Date expiration) throws ParseException {
 DateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy");
 Date date = dateFormat.parse(input);
 return date.after(expiration);
}

代码示例来源: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

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

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

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

代码示例来源:origin: square/okhttp

@Override protected DateFormat initialValue() {
  // Date format specified by RFC 7231 section 7.1.1.1.
  DateFormat rfc1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
  rfc1123.setLenient(false);
  rfc1123.setTimeZone(UTC);
  return rfc1123;
 }
};

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

/**
    * @{inheritDoc}
    */
 public Date parse(String source, ParsePosition pos) {
   dateFormat.setTimeZone(TimeZone.getDefault());
   return dateFormat.parse(source, pos);
 }
}

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

/**
   * @{inheritDoc}
   */
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
  dateFormat.setTimeZone(TimeZone.getDefault());
  return dateFormat.format(date, toAppendTo, fieldPosition);
}

代码示例来源:origin: org.apache.commons/commons-lang3

try {
  final TimeZone denverZone = TimeZone.getTimeZone("America/Denver");
  TimeZone.setDefault(denverZone);
  final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS XXX");
  format.setTimeZone(denverZone);
  final Date oct31_01MDT = new Date(1099206000000L);
  final Date oct31MDT = new Date(oct31_01MDT.getTime() - 3600000L); // - 1 hour
  final Date oct31_01_02MDT = new Date(oct31_01MDT.getTime() + 120000L);  // + 2 minutes
  final Date oct31_01_02_03MDT = new Date(oct31_01_02MDT.getTime() + 3000L);    // + 3 seconds
  final Date oct31_01_02_03_04MDT = new Date(oct31_01_02_03MDT.getTime() + 4L);       // + 4 milliseconds
  assertEquals("Check 00:00:00.000", "2004-10-31 00:00:00.000 -06:00", format.format(oct31MDT));
  assertEquals("Check 01:00:00.000", "2004-10-31 01:00:00.000 -06:00", format.format(oct31_01MDT));
  assertEquals("Check 01:02:00.000", "2004-10-31 01:02:00.000 -06:00", format.format(oct31_01_02MDT));
  assertEquals("Check 01:02:03.000", "2004-10-31 01:02:03.000 -06:00", format.format(oct31_01_02_03MDT));
  assertEquals("Check 01:02:03.004", "2004-10-31 01:02:03.004 -06:00", format.format(oct31_01_02_03_04MDT));
  final Calendar gval = Calendar.getInstance();
  gval.setTime(new Date(oct31_01MDT.getTime()));
  gval.set(Calendar.MINUTE, gval.get(Calendar.MINUTE)); // set minutes to the same value
  assertEquals("Demonstrate Problem", gval.getTime().getTime(), oct31_01MDT.getTime() + 3600000L);
} finally {
  TimeZone.setDefault(defaultZone);

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

Calendar c = Calendar.getInstance();
c.setTime(new Date(yourmilliseconds));
c.set(Calendar.MILLISECOND, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm.ss.SSS'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.format(c.getTime()));

代码示例来源: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: stackoverflow.com

UTC = TimeZone.getTimeZone("UTC");
TimeZone.setDefault(UTC);
final Calendar c = new GregorianCalendar(UTC);
c.set(1, 0, 1, 0, 0, 0);
c.set(Calendar.MILLISECOND, 0);
BEGINNING_OF_TIME = c.getTime();
c.setTime(new Date(Long.MAX_VALUE));
END_OF_TIME = c.getTime();
final SimpleDateFormat sdf = new SimpleDateFormat(ISO_8601_24H_FULL_FORMAT);
sdf.setTimeZone(UTC);
System.out.println("sdf.format(BEGINNING_OF_TIME) = " + sdf.format(BEGINNING_OF_TIME));
System.out.println("sdf.format(END_OF_TIME) = " + sdf.format(END_OF_TIME));
System.out.println("sdf.format(new Date()) = " + sdf.format(new Date()));
System.out.println("sdf.parse(\"2015-04-28T14:23:38.521Z\") = " + sdf.parse("2015-04-28T14:23:38.521Z"));
System.out.println("sdf.parse(\"0001-01-01T00:00:00.000Z\") = " + sdf.parse("0001-01-01T00:00:00.000Z"));
System.out.println("sdf.parse(\"292278994-08-17T07:12:55.807Z\") = " + sdf.parse("292278994-08-17T07:12:55.807Z"));

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

final DateFormat format = new SimpleDateFormat("E. M/d");
final String dateStr = "Thu. 03/01";
final Date date = format.parse(dateStr);

GregorianCalendar gregory = new GregorianCalendar();
gregory.setTime(date);

XMLGregorianCalendar calendar = DatatypeFactory.newInstance()
    .newXMLGregorianCalendar(
      gregory);

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

TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
SimpleDateFormat simpleDateFormat = 
    new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy", Locale.US);
simpleDateFormat.setTimeZone(timeZone);

System.out.println("Time zone: " + timeZone.getID());
System.out.println("default time zone: " + TimeZone.getDefault().getID());
System.out.println();

System.out.println("UTC:     " + simpleDateFormat.format(calendar.getTime()));
System.out.println("Default: " + calendar.getTime());

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

SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = isoFormat.parse("2010-05-23T09:01:02");

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

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
cal.setTime(sdf.parse("Mon Mar 14 16:02:37 GMT 2011"));// all done

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

TimeZone zone = TimeZone.getTimeZone("America/New_York");
DateFormat format = DateFormat.getDateTimeInstance();
format.setTimeZone(zone);

System.out.println(format.format(new Date()));

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

Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.set(2010, 7, 23);
end.set(2010, 8, 26);
Date startDate = start.getTime();
Date endDate = end.getTime();
long startTime = startDate.getTime();
long endTime = endDate.getTime();
long diffTime = endTime - startTime;
long diffDays = diffTime / (1000 * 60 * 60 * 24);
DateFormat dateFormat = DateFormat.getDateInstance();
System.out.println("The difference between "+
 dateFormat.format(startDate)+" and "+
 dateFormat.format(endDate)+" is "+
 diffDays+" days.");

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

Long gmtTime =1317951113613L; // 2.32pm NZDT
Long timezoneAlteredTime = 0L;

if (offset != 0L) {
  int multiplier = (offset*60)*(60*1000);
  timezoneAlteredTime = gmtTime + multiplier;
} else {
  timezoneAlteredTime = gmtTime;
}

Calendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(timezoneAlteredTime);

DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");

formatter.setCalendar(calendar);
formatter.setTimeZone(TimeZone.getTimeZone(timeZone));

String newZealandTime = formatter.format(calendar.getTime());

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

private String formatDate(long date) {
  return newDateFormat().format(new Date(date));
}

代码示例来源:origin: apache/kylin

private static String time(long t) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ROOT);
    Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.ROOT);
    cal.setTimeInMillis(t);
    return dateFormat.format(cal.getTime());
  }
}

相关文章