• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get current date and time

 
Sharma Vinit
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
make use of Calendar class in the java.util package
or make use of Date from the java.util package

java.util.Date()
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can also use
 
Sharma Vinit
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pankaj patil
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't look good. You are either returning true or throwing a checked Exception.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic