• Post Reply Bookmark Topic Watch Topic
  • New Topic

A little java.util.Date help  RSS feed

 
Ioanna Katsanou
Ranch Hand
Posts: 129
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a String that represents date, and I want to convert it to java.util.Date object in a specific format in java 7.

     

A java.text.ParseException: Unparseable date: "2017-02-22 23:00:00.0"
is thrown but I cannot understand why.
What am I doing wrong??
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you see any difference between your expected date format and the actual?
 
Ioanna Katsanou
Ranch Hand
Posts: 129
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Can you see any difference between your expected date format and the actual?


Yes, how can I change the date so it appears in this format?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I say to you: "Yesterday, a bit after 1 pm I went lunch".
Can you tell me please, how many minutes were after 1 pm?

Similar issues are with String you have.

[edit] Probably you are asking something slightly different. Apologies if I misunderstood.
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The text passed in to the SimpleDateFormat defines the expected format of the String representation of the date you want to represent. For example, you text format of "dd/MM/yyyy HH:mm:ss.SSS" expects your date to look something like "22/02/2017 23:00:00.000".
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait

Look to Tim's post.

Two things here need to be involved in order to get what you want. First - parsing, second - formatting.

When parsing you need to specify format in the way your string is, when formatting, accordingly how you want it to be. So - parse in exact format, then format in the way you want.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i.e.
 
Ioanna Katsanou
Ranch Hand
Posts: 129
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:i.e.


thanks i got it !!
 
Ioanna Katsanou
Ranch Hand
Posts: 129
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello (again)

I don't know, I am still facing a problem:

I have a method with a date parameter

for ex.

When I use it like this :



It works just fine !!!
But if I use it like this:

                
                  
                      In this second time  it does not work !!! why is this happening???
 
Ioanna Katsanou
Ranch Hand
Posts: 129
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did this:



I will try now if it works !
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In stead of using if else, how about maintaining a list of date formats?
 
Ioanna Katsanou
Ranch Hand
Posts: 129
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:In stead of using if else, how about maintaining a list of date formats?

What do you mean exactly??
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this.  This way its likely to become more generic, and might be able to parse more number of formats.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:
                      In this second time  it does not work !!! why is this happening???


It worked for me... meaning I got the correct Date instance from the formatter parse() method.

Henry
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:But if I use it like this
..
In this second time  it does not work
Can you please be more specific. What you mean by "doesn't work". For me code compiles, runs, parses (I just have added exception handling part, you probably re-throwing).
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It's probably failing due to three yyy?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:
It's probably failing due to three yyy?


Works for me -- even with the three yyys.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!