• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Simple Calendar question. Formatting output.

I am creating a log for files that are emailed through my program.  The only problem is that when the following statement executes it shows like this:

9:1:36

I want it to show

9:01:36

Calendar today = Calendar.getInstance();
System.out.println(today.get(Calendar.HOUR)+":"+today.get(Calendar.MINUTE)+":"+today.get(Calendar.SECOND));

The seconds also will show only one character when the seconds are between 0 and 9.  Thanks for any help.
0
Intern
Asked:
Intern
  • 4
  • 2
1 Solution
 
InternAuthor Commented:
Also one other simple thing.  I have a STRING variable that has the following in it "someone@domain.com"  I would like to print only the "someone" portion of the variable.  The variable will always contain "######@domain.com"
0
 
CEHJCommented:
SimpeDateFormat sdf = new SimpleDateFormat("h:mm:ss");
System.out.println(sdf.format(today.getTime()));
0
 
CEHJCommented:
s = s.substring(0, s.indexOf("@"));
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
CEHJCommented:
If you want *all* fields to have two digits:

 SimpeDateFormat sdf = new SimpleDateFormat("hh:mm:ss");

or in 24hr clock:

 SimpeDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
0
 
InternAuthor Commented:
Thank you very much for helping a Java newbie so quickly.
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now