-5

In my stock management project i want to save the time Product is saved. I want to save it in this format.
date + time(in miliseconds)

So I used this code, which is completely failed I think.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JLabel;

/**
*
* @author Dilini
*/
public class test {

public void Timemill(final JLabel jl) {
    new Thread(new Runnable() {
        public void run() {
            while (true) {
                DateFormat smFormat = new SimpleDateFormat("yyyyMMdd");
                Date date = new Date();
                String smday = smFormat.format(date);
                int itime = Calendar.getInstance().get(Calendar.MILLISECOND);
                jl.setText(smday + String.valueOf(itime));
            }
        }
    }).start();

 }
}


But result brings something like this.
20140530542
I know 2014=year 05=month 30=day. I can understand 542 is may be seconds. But seconds usually with 0-60. And 542 over that range.. I want to clarify what that is. Also I want to output time with more formatted way.. Thank you.

Dilini
  • 69
  • 4
  • 10

3 Answers3

3

I don't know what 542 is please help me.

It's the value of iTime. You're appending it...

jl.setText(smday+String.valueOf(itime));
// ...here -----^^^^^^^^^^^^^^^^^^^^^^

That value, in turn, comes from:

int itime = Calendar.getInstance().get(Calendar.MILLISECOND);

It's the milliseconds portion of the current date/time.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I knoe it is my dear T.J. Usually when we run date it brings this format 15:04:03 but this was just 3 numbers. That's why I asked. To be honest I copy pasted this "Calendar.getInstance().get(Calendar.MILLISECOND) method. I don't have good clear idea. That's why I asked why 542 brings. – Dilini May 30 '14 at 08:41
3

Run this and you will see how you can get all parts of a Date.

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    System.out.println(sdf.format(new Date()));

Then use it as appropriate for your particular case.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
-1

Use this format yyyyMMddHHmmssS For more information see the Documentation in SimpleDateFormat

H is the hour
m stand for minute
s for second
S for miliseconds.

542 are miliseconds. You get it here from the Calendar

int itime = Calendar.getInstance().get(Calendar.MILLISECOND);

Change.

         DateFormat smFormat = new SimpleDateFormat("yyyyMMdd");
            Date date = new Date();
            String smday = smFormat.format(date);
            int itime = Calendar.getInstance().get(Calendar.MILLISECOND);
            jl.setText(smday + String.valueOf(itime));

TO

            DateFormat smFormat = new SimpleDateFormat("yyyyMMddHHmmssS");
            Date date = new Date();
            String smday = smFormat.format(date);
            jl.setText(smday);

to format your date in a proper way.

Jens
  • 67,715
  • 15
  • 98
  • 113