24 hour in SimpleDateFormat() and milliseconds in java

12,082

Solution 1

Try this :-

SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSS");

Also read this. Hope it will help you.

Solution 2

You should use HH instead of hh:

SimpleDateFormat now = new SimpleDateFormat("HH:mm:ss");
Share:
12,082
Thanos
Author by

Thanos

I have just(October 2012) finished my undergraduate studies in Applied Physics(D.Sc., D.Eng.) at National Technical University of Athens(NTUA). My thesis was about the MicroMEGAS detector where I constructed, studied, simulated and analysed data. I have enrolled(October 2012) in a M.Sc. programm called Physics and Technological Applications (NTUA) and I hope to continue with a Ph.D. My working experience includes 7 years student's tutoring(university,high school, primary school), 1 year of developing laser systems on behalf of the NTUA Laser Group, 1 year of detector studies on behalf of the NTUA High Energy Physics Group(HEP-NTUA) and a month working at CERN on behalf of the RD51 collaboration, in detector RnD. My future plans include Ph.D studies in the USA or Europe in the field of experimental Physics(I should decide on which particular field), marrying my girlfriend and working in research. Other interests include culinary, pastry, photography, basketball, cinema, completing tutorial in various fields and computing.

Updated on June 28, 2022

Comments

  • Thanos
    Thanos almost 2 years

    I am building a DAQ software and right now I am in the process of creating a log file. I thought of showing the time on each entry. A simple code(that cannot compile) is the following

    import java.util.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    
    public class daq(){
    
        SimpleDateFormat now = new SimpleDateFormat("hh:mm:ss");
    
        public void go(){
            report.setProperty("INSERT", "["+now.format(new Date())+"] Acquisition Started\n");
        }
        public void init(){
            report.setProperty("INSERT", "["+now.format(new Date())+"] ADC Channel 1: Read Complete\n");
            report.setProperty("INSERT", "["+now.format(new Date())+"] Initialisation Complete\n");
        }
        public void stop(){
            report.setProperty("INSERT", "["+now.format(new Date())+"] Acquisition Stopped\n");
        }
    
    }
    

    A typical result of the above "code" is the following

    [06:30:09] Acquisition Started
    [06:30:09] ADC Channel 1: Read Complete
    [06:30:09] Initialisation Complete
    [06:30:13] Acquisition Stopped
    

    I was wondering if there is a way to display the time in 24 hour format(i.e. 18:30:09 instead of 06:30:09)?

    Is there also a way to display milliseconds? A format like hh:mm:ss:mils? I tried to use sss instead of ss but I only get a 0 for the first s. An example output is

    [21:50:004] Acquisition Started
    [21:50:004] ADC Channel 1: Read Complete
    [21:50:004] Initialisation Complete
    [21:50:013] Acquisition Stopped
    
  • Rohit Jain
    Rohit Jain about 10 years
    @Thanos Have you gone through the documentation of SimpleDateFormat? You'll get all the formats there. BTW, for milliseconds you use SSS.
  • Thanos
    Thanos about 10 years
    I have already tried sss but I don't actually get what I want. Please check my edited question.