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.

simpledateformat
Filter by
Sorted by
Tagged with
207 votes
5 answers
493k views

How to parse a date? [duplicate]

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 { ...
fenec's user avatar
  • 5,727
20 votes
3 answers
15k views

Java - Unparseable date

I am trying to parse a date, but I am oddly getting an exception. This is the code: import java.util.Date; String strDate = "Wed, 09 Feb 2011 12:34:27"; Date date; SimpleDateFormat FORMATTER = ...
Amokrane Chentir's user avatar
95 votes
5 answers
35k views

Y returns 2012 while y returns 2011 in SimpleDateFormat

I wonder why 'Y' returns 2012 while 'y' returns 2011 in SimpleDateFormat: System.out.println(new SimpleDateFormat("Y").format(new Date())); // prints 2012 System.out.println(new SimpleDateFormat("y")....
Eng.Fouad's user avatar
  • 116k
186 votes
23 answers
195k views

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 ...
Ken Hume's user avatar
  • 1,955
20 votes
4 answers
55k views

SimpleDateFormat ignoring month when parsing

The following code is giving me the parsed date as "Wed Jan 13 00:00:00 EST 2010" instead of "Wed Jun 13 00:00:00 EST 2010". Any ideas much appreciated. SimpleDateFormat sf = new ...
manu's user avatar
  • 263
231 votes
4 answers
204k views

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 ...
hguser's user avatar
  • 35.6k
69 votes
13 answers
141k views

How to parse dates in multiple formats using SimpleDateFormat

I am trying to parse some dates that are coming out of a document. It would appear users have entered these dates in a similar but not exact format. here are the formats: 9/09 9/2009 09/2009 9/1/...
Derek's user avatar
  • 11.8k
677 votes
12 answers
884k views

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....
Sarah Vessels's user avatar
267 votes
9 answers
182k views

Why is Java's SimpleDateFormat not thread-safe? [duplicate]

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 ...
Vivek Sharma's user avatar
  • 2,677
208 votes
9 answers
893k views

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 ...
Pradip Borde's user avatar
  • 2,536
67 votes
10 answers
378k views

java.text.ParseException: Unparseable date

I am getting a parsing exception while I am trying the following code: String date="Sat Jun 01 12:53:10 IST 2013"; SimpleDateFormat sdf=new SimpleDateFormat("MMM d, yyyy HH:mm:ss"); Date ...
Ami's user avatar
  • 4,251
100 votes
10 answers
267k views

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. ...
fiskeben's user avatar
  • 3,445
101 votes
12 answers
207k views

SimpleDateFormat parsing date with 'Z' literal [duplicate]

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 ...
DanInDC's user avatar
  • 5,129
22 votes
5 answers
57k views

SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"

I am trying to parse a String (YYYY-MM-dd HH:mm) to Date, however getting wrong date than expected. CODE: Date newDate = null; String dateTime = "2013-03-18 08:30"; SimpleDateFormat df = new ...
Sas's user avatar
  • 2,503
23 votes
4 answers
16k views

Java SimpleDateFormat always returning January for Month

I'm working on taking a date value (createWhen) from Active Directory, and translating it into a Java date, for the purposes of getting a list of accounts created between two dates. Everything is ...
EricBoersma's user avatar
  • 1,019
35 votes
4 answers
60k views

Java date parsing with microsecond or nanosecond accuracy

According to the SimpleDateFormat class documentation, Java does not support time granularity above milliseconds in its date patterns. So, a date string like 2015-05-09 00:10:23.999750900 // The ...
PNS's user avatar
  • 19.6k
23 votes
8 answers
97k views

Convert String Date to String date different format [duplicate]

I am new to Java. Postgres db contain date format is yyyy-MM-dd. I need to convert to dd-MM-yyyy. I have tried this, but wrong result is display public static void main(String[] args) throws ...
Murugesapillai Nagkeeran's user avatar
1 vote
4 answers
2k views

Getting error java.text.ParseException: Unparseable date: (at offset 0) even if the Simple date format and string value are identical

I'm always getting the parse exception even if the format to check and the string value are same. Here is the code: String format = "EEE MMM dd HH:mm:ss z yyyy"; String value = "Mon Sep 18 10:30:06 ...
sai's user avatar
  • 97
70 votes
10 answers
350k views

How to convert a String to a Date using SimpleDateFormat?

I have this code snippet: DateFormat formatter1; formatter1 = new SimpleDateFormat("mm/DD/yyyy"); System.out.println((Date)formatter1.parse("08/16/2011")); When I run this, I get this as the output: ...
Shantanu Tomar's user avatar
38 votes
2 answers
28k views

How does Java "week year" really work?

This started as a simple error: I had YYYY instead of yyyy in my format string for a SimpleDateFormat object. But I'm totally baffled by the results of my tests with the incorrect format string. This ...
Dave Mulligan's user avatar
21 votes
5 answers
19k views

Parsing a date’s ordinal indicator ( st, nd, rd, th ) in a date-time string

I checked the SimpleDateFormat javadoc, but I am not able to find a way to parse the ordinal indicator in a date format like this: Feb 13th 2015 9:00AM I tried "MMM dd yyyy hh:mma", but the days ...
hao's user avatar
  • 645
8 votes
2 answers
20k views

Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java [duplicate]

I am trying to parse a String using SimpleDateFormat. This is my current code: public String getCreatedDateTime() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddEHH:mm:ss....
Guy's user avatar
  • 6,493
10 votes
4 answers
40k views

Changing String date format

my current date format is : 08/11/2008 00:00 I need to convert this output to 2008/11/08 00:00 However, using the SimpleDateFormat as researched it is unable to do so and give me a totally ...
Cosq's user avatar
  • 155
6 votes
7 answers
32k views

Date object SimpleDateFormat not parsing timestamp string correctly in Java (Android) environment

I'm using the SimpleDateFormat object with the Date object as shown below. The problem lis that the Date object shows the wrong date, which is a few minutes off from the original string. The Date ...
Chirag Patel's user avatar
  • 5,899
24 votes
6 answers
45k views

Unable to parse DateTime-string with AM/PM marker

The string I want to format looks like this: String datetime = "9/1/10 11:34:35 AM" Following pattern for SimpleDateFormat works: SimpleDateFormat sdf = SimpleDateFormat("M/d/yy h:mm:ss"); Date d = ...
aksamit's user avatar
  • 2,385
18 votes
3 answers
86k views

Date format parse exception - "EEE MMM dd HH:mm:ss Z yyyy" [duplicate]

I got problem with date parse example date: SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.getDefault()); parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013"); ...
PDS's user avatar
  • 572
209 votes
8 answers
427k views

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 ...
Doug Molineux's user avatar
62 votes
3 answers
130k views

SimpleDateFormat parse loses timezone [duplicate]

Code: SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z"); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); System.out.println(new Date()); try { String d = sdf....
Achow's user avatar
  • 8,628
18 votes
5 answers
7k views

Java Date year calculation is off by year for two days

This caused a Y2K-style bug in my software if you can imagine. Strange thing is the off-by-one year calculation only occurs for two days in the year, which I'm less sure how to troubleshoot. The ...
djechlin's user avatar
  • 60.1k
4 votes
3 answers
3k views

SimpleDateFormat returns wrong time zone during parse

I am facing a strange problem during the creation of Date object. I have the below code to set the time zone of the date to UTC. Before the parse statement i see that sdfDate has time zone as UTC but ...
user2293813's user avatar
158 votes
8 answers
394k views

Get yesterday's date using Date [duplicate]

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"); ...
AKIWEB's user avatar
  • 19.4k
12 votes
7 answers
15k views

SimpleDateFormat parse(string str) doesn't throw an exception when str = 2011/12/12aaaaaaaaa?

Here is an example: public MyDate() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d"); sdf.setLenient(false); String t1 = "2011/12/12aaa"; System.out....
Terminal User's user avatar
43 votes
6 answers
262k views

How to convert date in to yyyy-MM-dd Format?

Sat Dec 01 00:00:00 GMT 2012 I have to convert above date into below format 2012-12-01 How can i? i have tried with following method but its not working public Date ConvertDate(Date date){ ...
BhavikKama's user avatar
  • 8,674
23 votes
2 answers
56k views

Java SimpleDateFormat Timezone offset with minute separated by colon

How can I get my date formatted as 2012-11-25T23:50:56.193+01:00 using SimpleDateFormat? If I use Z in the format like yyyy-MM-dd'T'hh:mm:ss.SSSZ then it shows 2013-03-06T11:49:05.490+0100
user2139437's user avatar
16 votes
4 answers
79k views

How to Parse Date from GMT TimeZone to IST TimeZone and Vice Versa in android

I am working on a project that fetches Date/Time from backend in IST(Indian standard Time) as shown "2013-01-09T19:32:49.103+05:30". However when i parse it using following DateFormat DateFormat sdf ...
Amritpal Singh's user avatar
1 vote
3 answers
485 views

Comparing time is incorrect when picking 12:00

I am creating a booking system and I don't want to allow users to book with starting time 11:00 and end time 09:00 (past)(I am using 24hour clock). I have two combo boxes filled with Strings that act ...
hDDen's user avatar
  • 145
75 votes
3 answers
184k views

How to parse month full form string using DateFormat in Java?

I tried this: DateFormat fmt = new SimpleDateFormat("MMMM dd, yyyy"); Date d = fmt.parse("June 27, 2007"); error: Exception in thread "main" java.text.ParseException: Unparseable date: "June 27, ...
tommy chheng's user avatar
  • 9,158
37 votes
6 answers
124k views

Java Convert GMT/UTC to Local time doesn't work as expected

In Order to show a reproducible scenario, I am doing the following Get the current system time (local time) Convert Local time to UTC // Works Fine Till here Reverse the UTC time, back to local time. ...
Vinod Jayachandran's user avatar
9 votes
3 answers
11k views

Generic support for ISO 8601 format in Java 6

Java 7 has introduced support in the SimpleDateFormat class for ISO 8601 format, via the character X (instead of lower or upper case Z). Supporting such formats in Java 6 requires preprocessing, so ...
PNS's user avatar
  • 19.6k
0 votes
2 answers
827 views

Converting String to Date using SimpleDateFormat is returning random date [duplicate]

I'm very confused by the following behaviour. I am returning 2 dates as Strings from a method: getLastSupplierFlightResults() I have added a screenshot showing the returned dates as "2018-06-20 00:...
Steerpike's user avatar
  • 1,783
99 votes
4 answers
323k views

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's user avatar
81 votes
11 answers
79k views

Displaying AM and PM in lower case after date formatting

After formatting a datetime, the time displays AM or PM in upper case, but I want it in lower case like am or pm. This is my code: public class Timeis { public static void main(String s[]) { ...
xrcwrn's user avatar
  • 5,359
2 votes
1 answer
1k views

12:xx shown as 00:xx in SimpleDateFormat.format("hh:mm:ss")

When using SimpleDateFormatter.format in the following code, hours between 12:00 and 12:59 are shown as 00:00 to 00:59 in startDateText TextView, while since 13:00 on they are correctly shown as 13:xx,...
sthor69's user avatar
  • 648
216 votes
31 answers
651k views

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 ...
Chatar Veer Suthar's user avatar
25 votes
6 answers
151k views

How to convert HH:mm:ss.SSS to milliseconds?

I have a String 00:01:30.500 which is equivalent to 90500 milliseconds. I tried using SimpleDateFormat which give milliseconds including current date. I just need that String representation to ...
Ahamed's user avatar
  • 39.6k
24 votes
5 answers
68k views

String-Date conversion with nanoseconds

I've been struggling for a while with this piece of code for an Android app and I can't get the hang of it. I've read and tried every solution I found on stackoverflow and other places, but still no ...
Rhadoozooz's user avatar
7 votes
3 answers
6k views

Getting wrong month when using SimpleDateFormat.parse

In my programm there is a very strange problem. Here you can see String birthday and Log to check it: birthday = String.valueOf(birthYear) + "-" + String.valueOf(birthMonth) + "-" + String.valueOf(...
Sergius's user avatar
  • 521
4 votes
2 answers
12k views

how to add days to java simple date format

How should I add 120 days to my current date which I got using simple date format? I have seen few posts about it but couldn't get it to work, My code is below: SimpleDateFormat dateFormat = new ...
user3315606's user avatar
4 votes
5 answers
12k views

SimpleDateFormat.parse() ignores the number of characters in pattern

I'm trying to parse a date String which can have tree different formats. Even though the String should not match the second pattern it somehow does and therefore returns a wrong date. That's my code: ...
das Keks's user avatar
  • 3,871
3 votes
2 answers
3k views

Simple Date format returns Wrong date intermittently [duplicate]

I am converting Date to string format in yyyy-MM-dd HH:mm:ss format to save in sqlite database below is object declared for simple date format public static SimpleDateFormat sdf = new ...
Deepak Sharma's user avatar

1
2 3 4 5
10