Questions tagged [simpledateformat]

SimpleDateFormat is a concrete Java class for formatting and parsing dates and times in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.

See documentation on SimpleDateFormat.

2636 questions
656
votes
12 answers

What is this date format? 2011-08-12T20:17:46.384Z

I have the following date: 2011-08-12T20:17:46.384Z. What format is this? I'm trying to parse it with Java 1.4 via DateFormat.getDateInstance().parse(dateStr) and I'm getting java.text.ParseException: Unparseable date:…
Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222
266
votes
9 answers

Why is Java's SimpleDateFormat not thread-safe?

Please tell with a code example why is SimpleDateFormat not threadsafe. What is the problem in this class? Is The problem with format function of SimpleDateFormat? Please give a code which demonstrates this fault in class. FastDateFormat is…
Vivek Sharma
  • 2,667
  • 2
  • 15
  • 8
228
votes
4 answers

Illegal pattern character 'T' when parsing a date string to java.util.Date

I have a date string and I want to parse it to normal date use the java Date API,the following is my code: public static void main(String[] args) { String date="2010-10-02T12:23:23Z"; String pattern="yyyy-MM-ddThh:mm:ssZ"; …
hguser
  • 35,079
  • 54
  • 159
  • 293
214
votes
31 answers

How can I get current date in Android?

I wrote the following code Date d = new Date(); CharSequence s = DateFormat.format("MMMM d, yyyy ", d.getTime()); I want the current date in string format, like 28-Dec-2011 so that I can set it into a TextView.
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154
206
votes
8 answers

Convert String to Calendar Object in Java

I am new to Java, usually work with PHP. I am trying to convert this string: Mon Mar 14 16:02:37 GMT 2011 Into a Calendar Object so that I can easily pull the Year and Month like this: String yearAndMonth =…
Doug Molineux
  • 12,283
  • 25
  • 92
  • 144
206
votes
9 answers

Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST

I have SimpleDateFormat constructor as SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") and I am parsing string "2013-09-29T18:46:19Z". I have read that here Z represents the GMT/UTC timezone. but when I print this date on console , It prints IST…
Pradip Borde
  • 2,516
  • 4
  • 18
  • 20
204
votes
5 answers

How to parse a date?

I am trying to parse this date with SimpleDateFormat and it is not working: import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Formaterclass { public static void main(String[] args) throws…
fenec
  • 5,637
  • 10
  • 56
  • 82
184
votes
23 answers

How do you format the day of the month to say "11th", "21st" or "23rd" (ordinal indicator)?

I know this will give me the day of the month as a number (11, 21, 23): SimpleDateFormat formatDayOfMonth = new SimpleDateFormat("d"); But how do you format the day of the month to include an ordinal indicator, say 11th, 21st or 23rd?
Ken Hume
  • 1,935
  • 2
  • 13
  • 7
171
votes
6 answers

Is there a date format to display the day of the week in java?

I know of date formats such as "yyyy-mm-dd" -which displays date in format 2011-02-26 "yyyy-MMM-dd"-which displays date in format 2011-FEB-26 to be used in eg: SimpleDateFormat formatter = new SimpleDateFormat( "yyyy/MMM/dd "); I…
rogerstone
  • 7,541
  • 11
  • 53
  • 62
159
votes
3 answers

How to convert an Instant to a date format?

I can convert a java.util.Date to a java.time.Instant (Java 8 and later) this way: Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 8); cal.set(Calendar.MINUTE, 30); Date startTime = cal.getTime(); Instant i =…
hari m
  • 1,715
  • 2
  • 10
  • 7
156
votes
8 answers

Get yesterday's date using Date

The following function produces today's date; how can I make it produce only yesterday's date? private String toDate() { DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); return…
AKIWEB
  • 19,008
  • 67
  • 180
  • 294
120
votes
13 answers

Why is the month changed to 50 after I added 10 minutes?

I have this date object: SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd HH:mm"); Date d1 = df.parse(interviewList.get(37).getTime()); value of d1 is Fri Jan 07 17:40:00 PKT 2011 Now I am trying to add 10 minutes to the date above. Calendar…
junaidp
  • 10,801
  • 29
  • 89
  • 137
99
votes
10 answers

SimpleDateFormat and locale based format string

I'm trying to format a date in Java in different ways based on the given locale. For instance I want English users to see "Nov 1, 2009" (formatted by "MMM d, yyyy") and Norwegian users to see "1. nov. 2009" ("d. MMM. yyyy"). The month part works OK…
fiskeben
  • 3,395
  • 4
  • 31
  • 35
98
votes
12 answers

SimpleDateFormat parsing date with 'Z' literal

I am trying to parse a date that looks like this: 2010-04-05T17:16:00Z This is a valid date per http://www.ietf.org/rfc/rfc3339.txt. The 'Z' literal (quote) "imply that UTC is the preferred reference point for the specified time." If I try to…
DanInDC
  • 5,019
  • 8
  • 31
  • 25
98
votes
4 answers

What are the date formats available in SimpleDateFormat class?

Can anybody let me know about the date formats available in SimpleDateFormat class. I have gone through api but could not find a satisfactory answer.Any help is highly appreciated.
Abhishek_Mishra
  • 4,551
  • 4
  • 25
  • 38
1
2 3
99 100