• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..)
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the classes DateFormat and SimpleDateFormat.
 
Ethan Bauer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also do something like this (I do this in one of my projects):

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic