| Author |
How to get current date and time
|
Sharma Vinit
Greenhorn
Joined: Apr 29, 2010
Posts: 5
|
|
hi friends,
I wish to use the current year in my program but unable to figure out a way to do this.There's no direct method in the Java docs.
How can i retrieve the current month and year in my program?
Thanks
|
 |
pankaj patil
Ranch Hand
Joined: Dec 19, 2004
Posts: 98
|
|
make use of Calendar class in the java.util package
or make use of Date from the java.util package
java.util.Date()
|
Regards,
Pankaj Patil
|
 |
Pramod P Deore
Ranch Hand
Joined: Jul 15, 2008
Posts: 629
|
|
you can also use
|
Life is easy because we write the source code.....
|
 |
Sharma Vinit
Greenhorn
Joined: Apr 29, 2010
Posts: 5
|
|
hey guys
how can i compare a date with current date. I have to check the joining date of a student which is received from front end as a string.The joining date should not be greater than today's date.
I have tried the following. Its working but i think there must be a better way out
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
Please UseCodeTags when you post source code.
That looks a lot more complicated than it needs to be...
You can use class java.text.SimpleDateFormat to parse a string into a Date object. Then you can use the methods after() or before() of class Date to check if the date is before or after another date.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
pankaj patil
Ranch Hand
Joined: Dec 19, 2004
Posts: 98
|
|
Look for Date comparison code in java
visit below mention link
http://www.roseindia.net/java/example/java/util/CompareDate.shtml
|
 |
Pradeep Katipamula
Ranch Hand
Joined: May 11, 2010
Posts: 37
|
|
Sharma Vinit wrote:
I have tried the following. Its working but i think there must be a better way out
Here's the code.
Hope this is wat you wanted. I;ve returned a boolean, if you really need to send a string please modify.
Pradeep
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
That doesn't look good. You are either returning true or throwing a checked Exception.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
DateFormat.parse(String, ParsePosition) returns null when the string is invalid. That can be used:
Not only does this code not throw an exception, it also doesn't create an unnecessary Calendar (and Date from getTime()) object. You can improve this code slightly by checking if the entire String is used in parsing:
If the parse position's index is not directly at the end of the String there is something else in the String as well. This will occur if the passed String is something like "20/05/2010abc". The added check will make the method return false if that's the case.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: How to get current date and time
|
|
|