SimpleDateFormat « SimpleDateFormat « Java Data Type Q&A

1. How can I insert special characters in SimpleDateFormat?

I want to insert the word 'at' in a SimpleDateFormat so a date would look like:

Wed, 26 May 2010 at 11:17am
I'm able to make it appear without the at, ...

2. SimpleDateFormat

The following code is giving me the parsed date as "Wed Jan 13 00:00:00 EST 2010" instead of "Wed Jun 13 ...

3. Method not found error in SimpleDateFormat

import java.util.*;    
import java.text.*;

public class GetPreviousAndNextDate
    public static void main(String[] args) 

4. java SimpleDateFormat behavior

I have a SimpleDateFormat with the pattern yyyy-M-d", and the following scenario:

String str = "02-03-04";        
SimpleDateFormat f = new SimpleDateFormat("yyyy-M-d");
The output is Sat Mar ...

5. SimpleDateFormat giving ParseException for unknown reason

I need to format my date (in Java) which is in String format. When I use the following code, taken from an tutorial/reference source, it just gives the following ...

6. Java SimpleDateFormat returns unexpected result

I'm trying to use SimpleDateFormat of Java to parse a String to date with the following code.

public class DateTester {

    public static void main(String[] args) throws ParseException {

7. How to apply applyPattern for SimpleDateFormat in java?

I am trying to add a new pattern to the date display but I am not getting the result that I am expecting: Here is my code:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");

8. applyPattern() in SimpleDateFormat gives NullPointerEcxeption with white space in java

I have this code that formats the date to this pattern dd-MM-yyyy:

     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
     Date date_out ...

9. Why is 2010/08/15 matching to M/d/yy?

I have an array of valid date formats I want to detect in a some text a user enters:

public static final DateFormat[] DATE_FORMATS = {
    new SimpleDateFormat("M/d/yy"),

10. Odd behavior of SimpleDateFormat


SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYYMMdd", Locale.US);
Object o = simpleDateFormat.parse("20111216");
Why? Giant bug?

11. SimpleDateFormat

Hello need some help ! i take in a String that looks like timePattern I just wonder how i change the output format when converting this if i make a print out on the result now i got Thu Nov 25 00:00:00 CET 2004 if i want for example 2004-11-30 12:33:35 how do i do that. String timePattern = "yyyy-MM-dd"; SimpleDateFormat ...

12. SimpleDateFormat problem

13. java.text.SimpleDateFormat does not behaves as expected

Hi, See the following code SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); sdf.setLenient(false); Date st = sdf.parse("04-12-2000sdfsdfsf"); System.out.println(st); It will produce output as Mon Dec 04 00:00:00 IST 2000 instead of throwing ParseException and also if I give SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); sdf.setLenient(false); Date st = sdf.parse("04-12-200046649sdfsdfsf"); System.out.println(st); It will produce output as Tue Dec 04 00:00:00 IST 200046649 It ignores trailing ...

14. SimpleDateFormat problem....

15. SimpleDateFormat

Hello need some help ! i take in a String that looks like timePattern I just wonder how i change the output format when converting this if i make a print out on the result now i got Thu Nov 25 00:00:00 CET 2004 if i want for example 2004-11-30 12:33:35 how do i do that. String timePattern = "yyyy-MM-dd"; SimpleDateFormat ...

16. A SimpleDateFormat for ISO 8601

In an XML file, a date is formatted using ISO 8601 convention. For example: 2005-04-01T13:38:09-08:00 The last part, "-08:00", is the timezone. How to parse such a string? If I use SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"), I can only parse 2005-04-01T13:38:09-0800 Any other way besides removing the last colon then use the SimpleDateFormat above? How to construct a SimpleDateFormat or just DateFormat so that it ...

17. SimpleDateFormat seems to be adding an hour

I'm sure this must be something daft on my part. When the expected output is 00:03:16.492, I'm getting 01:03:16.492 When the expected output is 01:19:56.492, I'm getting 02:19:56.492 If I try setting the time to 0 I get 01:00:00.000 Here is the code; public class Millis { public static void main(String[] args) { Date d = new Date(); SimpleDateFormat form = ...

18. SimpleDateFormat

19. SimpleDateFormat

20. Problem with SimpleDateFormat

21. SimpleDateFormat

I'm having an issue with a web app I'm working on where it reads in a date from a mainframe DB, parses it, then displays it on a page. The problem is the date comes in as a resultset string. the Format is "2006111" Where it's the yyyy and Day of the Year. I can read it in, parse it, and ...

22. SimpleDateFormat

How can I format a date to get a string of the following format: dd.m.y I have tried SimpleDateFormat sdf = new SimpleDateFormat("dd.M.y"); and sdf.format(myDate); This returns a string of the format dd.m.yy I dont want the 2 digit year. I only want the last digit of the year e.g. for 2007 I want 7.

23. Does SimpleDateFormat have any dependency on JDK versions

Hi All, Does SimpleDateFormat have any dependency on JDK versions? I am encountering the following issue: I have a java file named as The contents of this file is as follows: package com.test.util; import java.text.SimpleDateFormat; /** * Class that contains string contants that can be used in other classes. * */ public final class Constants { // Logging messages public ...

24. Question about SimpleDateFormat

25. Issue with SimpleDateFormat

I am trying to use the SimpleDateFormat class to validate a date entered by a user in a web app. Unfortunately, when I test it by putting an invalid date or month, it still computes the date and returns it as a valid date. Here is a test code to prove my point: public static void main(String[] args) { //String pattern ...

26. SimpleDateFormat and Ordinals

27. !SimpleDateFormat

It's the old array issue... create an array of month names and then use the month to get them. If the month is 1-12 then you have to subtract one. Since the months are 0-11, you don't have to subtract one. I'm sure Java does this because all the other languages do it.

28. SimpleDateFormat

29. SimpleDateFormat

30. What's wrong with my SimpleDateFormat?

31. How do I import SimpleDateFormat?

bash-2.03$ cat import*; import java.util.*; public class TimeStamp { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMdd" ); System.out.println( "ts = [" + sdf + "]" ); } } bash-2.03$ javac -verbose [parsing started] [parsing completed 221ms] [loading /usr/local/home/Plankton/SUNWappserver/jdk/jre/lib/rt.jar(java/lang/Object.class)] [loading /usr/local/home/Plankton/SUNWappserver/jdk/jre/lib/rt.jar(java/lang/String.class)] [checking TimeStamp] cannot resolve symbol symbol : class SimpleDateFormat location: class ...

32. SimpleDateFormat Problem

33. SimpleDateFormat

34. SimpleDateFormat

35. SimpleDateFormat

36. SimpleDateFormat (calculation)

Hi, I am trying to write a simple program which calculates how many days people have been lived in a house. I have data file named "People.txt" which has such structure: LastName FirstName CameIn CameOut DayCost I have written a program which makes the "Summary.txt" file like this: LastName FirstName CameIn CameOut DayCost But I need to get the structure of ...

37. SimpleDateFormat

38. SimpledateFormat class issue.

package util; import java.util.*; import java.text.*; public class Testit{ public static void main(String[] args){ new Testit().test(); } public void test(){ String input="2007-12-05"; Date d1=parseIt(input); Calendar c=Calendar.getInstance(); c.setTime(d1); System.out.println("yyyy="+c.get(c.YEAR)); o/p yyyy=2007 OK System.out.println("mm="+c.get(c.MONTH)); o/p mm=0 <--NOT OK System.out.println("dd="+c.get(c.DATE)); o/p dd=5 OK } public Date parseIt(String dateTobeParsed){ Date date=null; try{ DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd"); date = (Date)formatter.parse(dateTobeParsed); }catch(ParseException ps){System.out.println(ps);} return date; } ...

39. Doubt in SimpleDateFormat

Thanks for the reply, apologize for the confusion, let me refine the question. I want to know if its possible to display the date object in the same way as the String which contains the date, say for ex: Consider the string 26/08/1994 is it possible to display a date object in this fashion? A sample code will be very helpful, ...

40. Problem in SimpleDateFormat

41. simpledateformat problem

Jhon, There is no pattern available for output which you needed. In your code :SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); the capital Z denotes,for formatting you have been used RFC 822 time zone format. so if go for the Pacific Standard Time ie use small letter 'z' instead of capital SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"); you can get the output like this ...

42. SimpleDateFormat giving an error

43. SimpleDateformat

I m trying to get timestamp in this format SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.fffffffff"); but its giving Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'f' any help Actually i need to format this " 9/7/2010 0:00" to oracle timestamp format i.e yyyy-MM-dd HH:mm:ss.fffffffff .. how can i achieve this ? thanks

44. simpledateformat

45. Stumped on my SimpleDateFormat issue

I have to two strings that I am trying to convert into a date type. The first date works perfectly but the second date the month is not converting correctly. DateFormat df = new SimpleDateFormat("yyyy-mm-dd"); Date startWeek = null; Date endWeek = null; try { startWeek = df.parse("2011-01-30"); endWeek = df.parse("2011-02-05"); } catch (ParseException e) { e.printStackTrace(); } My results appear ...

46. simpleDateFormat Problems and questions

Ok so here is the deal I am attempting to take data entered from the keyboard in the format MM dd yyyy with the whitespace. Example:12 28 1973 GregorianCalendar now = new GregorianCalendar(); Date today = now.getTime(); GregorianCalendar birthDayGregorian = new GregorianCalendar(); System.out.println("Please enter your birthdate in the format "+ " mm dd yyyy: "); birthDayString = scan.nextLine(); SimpleDateFormat sdf = ...

48. SimpleDateFormat

I apologize for this noob-level question, but I'm angry at SimpleDateFormat and am wondering in anyone knows how to make it do what I want. My issue is that I'm using SimpleDateFormat to basically validate different types of dates. This usually works fine, except with the terminating field of a date. For example, I parse the String "08/21/2007" against the format ...

49. Doubt in SimpleDateFormat

Hi Here is the requirement "how to convert a string into date without changing its format" When i tried to do this using SimpleDateFormat : SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); Date date = formatter.parse(str); Output String 26/08/1994 gets parsed as Fri Aug 26 00:00:00 IST 1994. The format gets changed, is it possible to preserve the format? Thanks in advance

50. SimpleDateFormat throws FileNotFoundException?

But when I run it in debug mode in Eclipse, it always fails at the following line of code: SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); The following is some of the error message: Thread [main] (Suspended (exception FileNotFoundException)) URLClassPath$JarLoader.getJarFile(URL) line: not available URLClassPath$JarLoader.(URL, URLStreamHandler, HashMap) line: not available URLClassPath$ line: not available AccessController.doPrivileged(PrivilegedExceptionAction) line: not available [native method] URLClassPath.getLoader(URL) line: not available ...

52. SimpleDateFormat not that simple

Where does the conversion happen from "number of seconds from midnight" to a Date? You can't expect SimpleDateFormat to magically know what the number you specified means (does SimpleDateFormat even have a format(int) method?). You will need to do the conversion yourself (using a Calendar) and then feed the resulting Date object (obtained from Calendar.getTime()) to your hhmmss format to obtain ...

53. SimpleDateFormat Question

Running of the code below : // Formatter is initialized to parse MM/dd/yyyy SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); // User enters MM/DD/YY Date date = formatter.parse("11/15/07"); System.out.println("Date String: " + formatter.format(date)); // Formatter messes up the century Outputs 11/15/0007 Is there a way we can make SimpleDateFormat assume current century if the century information is missing. ( or any other API ...

54. SimpleDateFormat

hai to all iam new to core java In my application i have taken date textfield with dd-mm-yyyy and i have save the date in sql server through the application and iam checking the date with this condition. My problem is that if the condition false it has check for the second time but it's not working. date = form.parse(Dob1,ppos); if(form!=null) ...

55. SimpleDateFormat (calculation)

I am not sure if this is good(admins please correct me if I am wrong), but I do not create another topic with the same code, I have just changed the Subject (added Q) and I will ask my next question here. An admin will likely not reply. But no, this is not good. Should have created a different topic - ...

56. Is this a SimpleDateFormat bug?

paulcw wrote: There might be another way to do this (confirm that a string contains a valid date and only a valid date), but I don't know what it is. That would be the parse(String, ParsePosition) method (check the API docs). Give it a ParsePosition object which says "Start at the first position", then call the method. When it returns it ...

57. unexpected SimpleDateFormat output

58. Mysterious 7 hours in java.text.SimpleDateFormat

I'm not religious so I baulk at the use of god. If you can see past my satire my post was useful. If your program is to do soemething then it should do it correctly. Displaying method 1 when you are in method 2 is wrong and if this was a more serious application it would be confusing to a user. ...

59. SimpleDateFormat Question

60. Problem with SimpleDateFormat

61. what's the letter F means in SimpleDateFormat

pbrockway2 wrote: What about the following Monday and Tuesday? Some locales (mine, evidently) start the week on a Sunday, others on a Monday. F : Date 5: Tue Sep 30 10:09:03 CEST 2008 1: Wed Oct 01 10:09:03 CEST 2008 1: Thu Oct 02 10:09:03 CEST 2008 1: Fri Oct 03 10:09:03 CEST 2008 1: Sat Oct 04 10:09:03 CEST 2008 ...

62. SimpleDateFormat, missing an hour

When I run that code the output is "18:11:12". But I'm in a timezone which is 8 hours before GMT (actually 7 hours at the moment). You are in a different time zone which is also not GMT. However if you set the time zone of your SimpleDateFormat objects to GMT, you'll find you get the result you are looking for. ...

63. java.text.SimpleDateFormat with zones

Hi, Thank you for the response.The problem is this is one our client's log file which we are trying to parse using the java.text.SimpleDateFormat from our own java application. Hence not sure about +060. I guess the part after the . represent the zone , please correct me.But +060 not sure what this means and I dont think java.text.SimpleDateFormat has a ...

64. SimpleDateFormat and Timestamp does not exist in the 1.6 JDK (1.6.0_18)

An additional reason why I phrased my answer the way I did is that many beginners don't know how to read error message or don't read them at all (I'm not implying that you're a beginner, I simply didn't know about your skill-level). They simply see an error message and go into panic mode. In that case it helps to show ...

66. SimpleDateFormat Problem

67. SimpleDateFormat unexpected behaviour

I suspect the following excerpt: For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, to be responsible for this behaviour, but i thought parsing with lenient would avoid such a "mistake", and throw an exception because of the wrong number of digits. What is the best way to check that the year is ...

68. Unexpected SimpleDateFormat behavior.

69. SimpleDateFormat issue

70. SimpleDateFormat Issue!

Next, you may consider allowing the end user to type in a date without the proper leading zeros, then have your program insert them before submitting the page or redisplaying them. For example: If the user types 1/5/2009, you convert it to 01/05/2009 You may also consider enforcing a range for the year. For instance, no year less than 1900 or ...

71. How to put "\n" into SimpleDateFormat()

72. Problem in SimpleDateFormat

Initially I was returning String thats why the method name is wrong.But I have changed my code and now I want to return a Date object.Ok I get that point..Date object doesn't accept format but is it possible the change the format by using SimpleDateFormat and in doing that should it always return a String not a Date object(as Date object ...

73. Odd week number behavior in SimpleDateFormat

0: 2009.01.09 is in week number week-2009-02 1: 2009.01.08 is in week number week-2009-02 2: 2009.01.07 is in week number week-2009-02 3: 2009.01.06 is in week number week-2009-02 4: 2009.01.05 is in week number week-2009-02 5: 2009.01.04 is in week number week-2009-02 6: 2009.01.03 is in week number week-2009-01 7: 2009.01.02 is in week number week-2009-01 8: 2009.01.01 is in week ...

74. SimpleDateFormat display 3 digit year, help!

75. SimpleDateFormat problems

76. Issue using SImpleDateFormat

I am trying to use SimpleDateFormat to convert my date and time string that I have to a Here is the code snippet: // declaring testDate java.util.Date testDate = null; // Using to parse the string into a datetime field SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm a"); testDate = formatter.parse("08/01/2008 5:00 AM"); System.out.println("Date is :"+ testDate); System.out prints this: Date ...

77. Error SimpleDateFormat...

78. SimpleDateFormat bug?

79. FastDateFormat vs SimpleDateFormat

80. Calendar week with SimpleDateFormat

Hi, this has to do with the definition of the "first week" of a year. See also the Javadoc of the Calendar class. How the "yy" field is filled is independent of the "w" you have before, so it prints 07, as the date is in that year. I think you have to handle that situation by yourself on dates in ...

81. SimpleDateFormat problem

82. SimpleDateFormat problem

This should be simple, but I don't see what the problem is... I have this simple method to convert a string to a Date type. When I pass in 02/25/2008, the date returned contains 01/25/2008... SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); return sdf.parse(dateString); I do not see why it would be subtracting one from the month? Thanks in advance.

83. SimpleDateFormat issue

84. SimpleDateFormat issue

85. SimpleDateFormat question

86. simpleDateFormat question

87. SimpleDateFormat: what am I missing?

88. SimpleDateFormat.toLocalizedPattern()

Good advice with the getAvailableLocals actually. However there are still too many unknowns and potential 'red-herrings' with my technique in general.. I've gotten over the problem at the source, i.e. ensuring that the date I need to process is always the same format. Bit easier that way I think, given that I did have the luxury of doing this. Thanks for ...

89. Possible bug in SimpleDateFormat??

It doesn't make sense to me. How can 12/31/06 be considered the first week in '06? It should format it to "2006-53" or "2007-01". Otherwise, that date will overrun dates such as 1/1/06... Week 53 maybe incorrect, but there are dates that fall into that category and they should be handled properly. What's interesting is that parsing "2006-53" or "2007-01" both ...

91. SimpleDateFormat problem

92. SimpleDateFormat problem?

93. simpledateformat...not so simple

Anyway, what's really bizarre, is that apparently there is some weird algorithm in the parse method for determining what the 1st 2 digits are. For example, if the year is 89 it gets translated to 1989. 05 gets translated to 2005. OK, that seems to make sense. But how about this )Anything from 00 to 2026 gets translated to the ...

94. Java SimpleDateFormat Issue