This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Display Calendar.MINUTE & SECOND with a 0 before the value, if the value is between 0 and 9? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Display Calendar.MINUTE & SECOND with a 0 before the value, if the value is between 0 and 9?" Watch "Display Calendar.MINUTE & SECOND with a 0 before the value, if the value is between 0 and 9?" New topic
Author

Display Calendar.MINUTE & SECOND with a 0 before the value, if the value is between 0 and 9?

Ethan Bauer
Greenhorn

Joined: Sep 22, 2010
Posts: 18
Here's the code I have so far:


I've commented my problem in the source, so read that please.
"Dagens datum" is Swedish for "Today's date" and "Klockan är" is "The time is", just to clarify.


(I'll also ask one more question which is just a thought that popped up while I was writing this source:
Why is it that I have to assign Calendar.getInstance to a variable, in this case "cal", which I then use "get" on with arguments like
Calendar.MINUTE ? Why can't I just do something like:


Why can't I just bundle it, so to speak?

This is nothing important (to progress), but I was just wondering why it is this way..)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Look at the classes DateFormat and SimpleDateFormat.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Ethan Bauer
Greenhorn

Joined: Sep 22, 2010
Posts: 18
Wouter Oet wrote:Look at the classes DateFormat and SimpleDateFormat.


Alright, not too familiar with the documentation, it looks confusing to me, but I guess you gotta start somewhere, I mean it's documented for a reason :P
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Ethan Bauer wrote:I'll also ask one more question which is just a thought that popped up while I was writing this source:
Why is it that I have to assign Calendar.getInstance to a variable, in this case "cal", which I then use "get" on with arguments like
Calendar.MINUTE ? Why can't I just do something like:
Why can't I just bundle it, so to speak?

This is nothing important (to progress), but I was just wondering why it is this way..)


If you only wanted to get one value from your Calendar instance you could do that. However, Calendar.getInstance returns a new Calendar instance set to the time at which it was created, so if you used it three times in your code you might get unexpected results.
Assume the time at which you make the first call is 11:59:59, but the time ticks over to 12:00:00 before you make your second and third calls.
With your code as it is your Calendar instance will represent the time 11:59:59 and it will show the correct time. However, if you used this codethen the time shown will be 11:00:00.
Hours = 11 from the first calendar instance
Minutes = 0 from the second calendar instance
Seconds = 0 from the third calendar instance.


Joanne
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 147

You could also do something like this (I do this in one of my projects):



Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

Jan Hoppmann wrote:

Oh no you didn't. You know to use StringBuffer (StringBuilder is better if your JVM version allows it), yet you then use + inside. Why not use this:
After all, currently using + with strings creates a StringBuilder. Your code is equivalent to this:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Rob Prime wrote:Oh no you didn't.
-XX:-DoEscapeAnalysis to the rescue
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 147

Ah, thanks Rob, hadn't thought much about that when I wrote this piece of code, also I wasn't aware that + creates a StringBuilder. I will fix that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display Calendar.MINUTE & SECOND with a 0 before the value, if the value is between 0 and 9?