• Post Reply Bookmark Topic Watch Topic
  • New Topic

Coding my own GetDateTime Class ?  RSS feed

 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have been experimenting with Calendar class and thought I would write my own GetDateTime class using formats that I would use most regularly.

I was surprised by a few things, e.g. format(cal.getTime()) returns type string !

QUESTION (1): My test-driver does not return any result, usually Eclipse tells me where my errors are, but not this time. What have I done wrong?

Here is the class code:


Here is the code snippet for another class file that I was experimenting with:


QUESION (2) : I wouldn't like to rely on the exact format of the incoming date string "strDate", is there a Java app that can parse and identify the format of the incoming date string.



 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi AhFai,

Regarding Question 1:
I didn't understand what exactly are you trying to do. Especially with line:

Anyways, if you look carefully, what are you doing is - sending a DateFormat object to format method of DateFormat.
Now, DateFormat class doesn't have any such method. So compiler tries to find such method in its parent class - which is Format class.
Format class has a matching method - but not the one accepting DateFormat as argument, but a method accepting Object as argument.
Since DateFormat IS-A Object, compiler is fine with this.

Hence, code compiles.

However, at runtime, the Format class' format method (accepting Object) calls an abstract method - which again goes to DateFormat class and there we see code which checks whether the input argument IS-A Date or IS-A Number.
In your case, it is neither (it is DateFormat) and hence we get IllegalArgumentException: Cannot format given Object as a Date

Regarding Question 2:
As per my knowledge, there is no such facility (at least not in SimpleDateFormat). If your code is expected to parse dates in different formats then I think you should be owner of the code which at least checks:
1) Whether input text is a valid date (e.g. what about 01-01-19blah?)
2) Invoke proper parser (it can be done by ugly way in which you'll create a bunch of parsers and try them all on input)
If you don't provide any format to SimpleDateFormat, it takes format SHORT as default.

I hope this helps.
 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:Hi AhFai,

Thanks for your input.

I was simply trying to pass a date object, e.g. cal.getTime(), from main() to the MyDateTime class for formatting by SimpleDateFormat() and return the formatted result. I seem to have run afoul of everything in the Calendar class at the moment.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was simply trying to pass a date object, e.g. cal.getTime(), from main() to the MyDateTime class for formatting by SimpleDateFormat() and return the formatted result.


Ok, let's start there.
How do you pass the date object from main to the MyDateTime class? Or how do you think it should happen?

i.e. What do you want the calling code to look like? (in other words write a test)
 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:
I was simply trying to pass a date object, e.g. cal.getTime(), from main() to the MyDateTime class for formatting by SimpleDateFormat() and return the formatted result.


Ok, let's start there.
How do you pass the date object from main to the MyDateTime class? Or how do you think it should happen?

i.e. What do you want the calling code to look like? (in other words write a test)


Well... this is how I think it should happen, in theory at least.:

I write a class file with constructors and getters
Then I write a test driver, which could be in a separate Java file

This is how I would write MyDateTime class

public class {

// CONSTRUCTORS
public MyDateTime() {
}
public MyDateTime(Date anyDateTime) {

}

// GETTERS


}
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so you have a Class, and a Test class to drive it.


This is the test driving code that you wrote previously:


And you stated the goal was:
...to pass a date object, e.g. cal.getTime(), from main() to the MyDateTime class for formatting by SimpleDateFormat() and return the formatted result.


That test code does not pass a date object anywhere. It doesn't even include one.

So my suggestion would be to rewrite your test driving code so that it matches your stated goal.

Then rewrite your class so that it matches your test.

In that order of course :-)
 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that was terrible
Sorry about that.
I am going to make this simple for myself, one step at a time.
I am going to assume I will be getting a date string and I am passing it to my date formatter class.
Here is the class ADateFormatter:


Here is the test driver



Here is the output : "Formatted string date : Tue Dec 1 12:00:00 UTC 2015"

It didn't format the date string the way I wanted it to, which is yyyy/MM/dd HH:mm:ss

The out put should be 2015/12/01 12:00:00

Might it solve my problem if I convert the incoming date string to type Date, pass it to the DateFormatter, and then use SimpleDateFormat to format it and than convert the formatted date back to type string?

 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

OK, it is very easy to convert incoming Date to "string" in Oracle and SQL Server ( wonder how mySQL handles it? )

So I'll assume everything coming in will be "strings" with caveats.

In the DateFormatter class, if I convert the string to Calendar, I can use SimpleDateFormat() method to convert it to a format that is required...

Does the appraoch sound right to you?

Should I be doing the String to Calendar conversion in the setter?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your DateFormatter object need to encapsulate a Date (String or otherwise) inside it? What does it use it for?
Your handling of passing a parameter to the constructor, and passing via the setter is different, even though both ostensibly "set" the value.
You are passing a String object to the format method which isn't going to work how you want it to. SimpleDateFormat.format() method takes a Date as its parameter. Because you pass a String, it actually goes to the generic format method, and thus doesn't do what you want it to.

The point of the DateFormatter class is to end up with a Date string right?
If you already have a Date String what is the point of the DateFormatter class?

What is the format of this Date String you have already, and do you need to use the parse function with it ?

>OK, it is very easy to convert incoming Date to "string" in Oracle and SQL Server ( wonder how mySQL handles it? )

Yes, yes it is. However the format of THAT string might be different based upon the settings in the database.
Generally I prefer to deal with actual Date objects coming out of a database rather than its formatted String.



This is what I had in mind for a Date Formatting object.
You ask it to format a date that you pass it as a parameter, and it returns you the String representing that date.



 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Does your DateFormatter object need to encapsulate a Date (String or otherwise) inside it? What does it use it for?

I was thinking about the various date formats I'll be getting e.g. USA vs European format, as a timestamp, with milliseconds and without, with or without local etc. They will almost always be in some text files, I will have to cast them into Date objects.

But isn't Date deprecated?

At the moment I cannot see how to encapsulate that using a DateFormatter class. If each DateFormatter class handles one date format there would be many such classes. If a DateFormatter handles all classes and returns a Date object that I need, it would be very messy.

I need the Date object so I can so I do things like cal.add(Calendar.Minute, 1)

It's not very OO, is it.

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