• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ethan Bauer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the classes DateFormat and SimpleDateFormat.
 
Ethan Bauer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also do something like this (I do this in one of my projects):

 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Oh no you didn't.
-XX:-DoEscapeAnalysis to the rescue
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic