Tabnine Logo
DateFormat
Code IndexAdd Tabnine to your IDE (free)

How to use
DateFormat
in
java.text

Best Java code snippets using java.text.DateFormat (Showing top 20 results out of 74,097)

Refine searchRefine arrow

  • SimpleDateFormat
  • Date
  • Calendar
  • TimeZone
  • GregorianCalendar
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());
  }
}
java.textDateFormat

Javadoc

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.

Most used methods

  • format
  • parse
  • getDateTimeInstance
  • setTimeZone
    Sets the time zone of the calendar used by this date format.
  • getDateInstance
    Returns a DateFormat instance for formatting and parsing dates in the specified style for the specif
  • getTimeInstance
    Returns a DateFormat instance for formatting and parsing time values in the specified style for the
  • setLenient
    Specifies whether or not date/time parsing shall be lenient. With lenient parsing, the parser may us
  • getInstance
    Returns a DateFormat instance for formatting and parsing dates and times in the SHORT style for the
  • clone
    Returns a new instance of DateFormat with the same properties.
  • setCalendar
    Sets the calendar used by this date format.
  • getTimeZone
    Returns the time zone of this date format's calendar.
  • getCalendar
    Returns the calendar used by this DateFormat.
  • getTimeZone,
  • getCalendar,
  • parseObject,
  • equals,
  • getAvailableLocales,
  • hashCode,
  • getNumberFormat,
  • isLenient,
  • formatToCharacterIterator,
  • setNumberFormat

Popular in Java

  • Updating database using SQL prepared statement
  • addToBackStack (FragmentTransaction)
  • findViewById (Activity)
  • getResourceAsStream (ClassLoader)
  • ObjectMapper (com.fasterxml.jackson.databind)
    ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Pl
  • HashSet (java.util)
    HashSet is an implementation of a Set. All optional operations (adding and removing) are supported.
  • UUID (java.util)
    UUID is an immutable representation of a 128-bit universally unique identifier (UUID). There are mul
  • ThreadPoolExecutor (java.util.concurrent)
    An ExecutorService that executes each submitted task using one of possibly several pooled threads, n
  • IOUtils (org.apache.commons.io)
    General IO stream manipulation utilities. This class provides static utility methods for input/outpu
  • Get (org.apache.hadoop.hbase.client)
    Used to perform Get operations on a single row. To get everything for a row, instantiate a Get objec
  • Top 12 Jupyter Notebook extensions
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now