• Post Reply Bookmark Topic Watch Topic
  • New Topic

DateFormat with Calendar  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DateFormat format = new SimpleDateFormat("hh:mm:ss");

Date now = format.parse("01:01:01");

Calendar cal = Calendar.getInstance();
cal.setTime(now);

System.out.println(cal.getTime());

It prints Mon Jan 01 01:01:01 IST 1970. How to print current date with specified hour, minute and seconds ?


 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, stop using Calendar and Date. They're old and they're clunky. Instead, use the java.time.* library.

LocalDate has the now() and atTime() methods. You can then print the resulting LocalDateTime.
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.time.LocalDate in java 8 and java 7 does not support java.time.LocalDate
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to output just hours, mins and seconds then use the DateFormat.format() method.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't you use Java 8?
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is limitation
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!