-1

I have a object that giving date and time in this format "2014-06-11 16:32:36.828". I want to remove millisec .828.

In my db that object is in time stamp format but whenever i am showing i am converting it to tostring().

so how to remove millisec please help me

Sushreesmita
  • 19
  • 1
  • 4
  • 2
    Do you mean that you want a String representation of your timestamp without milisec, or do you want to alter the timestamp (time value) to remove milisec? – Joeblade Nov 10 '14 at 09:30

7 Answers7

1
Date d = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(yourString);
Calendar c = Calendar.getInstance();
c.setTime(d);

c.set(Calendar.MILLISECOND, 0);
return c.getTime();

I remember there is a way to directly read Date off your timestamp field but I don't do that in my everyday coding. So I'd left for others to post so. Nevertheless, you can use the same above code to translate your date from that timestamp into a date without MILLISECOND.

Alex Suo
  • 2,977
  • 1
  • 14
  • 22
1

I would use DateUtils.truncate(date, Calendar.SECOND)

keuleJ
  • 3,418
  • 4
  • 30
  • 51
1

The following code convert "2014-06-11 16:32:36.828" into "2014-06-11 16:32:36"

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse("2014-06-11 16:32:36.828"));

Explanation:

  • new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse("2014-06-11 16:32:36.828") parse the input string into
    Wed Jun 11 16:32:36 IST 2014
  • new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) format the input date into specified structure.
Kumar
  • 3,782
  • 4
  • 39
  • 87
  • 11/10/14 15:46:04" how to separate date and time – Sushreesmita Nov 10 '14 at 10:21
  • 1
    String st=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse("2014-06-11 16:32:36.828")); System.out.println(st); int spaceIndex=st.indexOf(" "); String date=st.substring(0, spaceIndex); String time=st.substring(spaceIndex+1, st.length()); System.out.println(date+"\n"+time); – Kumar Nov 10 '14 at 10:27
0

If you receive it as a Timestamp, you should use the appropriate formatter when converting it to a string:

String s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);

Note: this will use the default time zone of the local computer.

assylias
  • 321,522
  • 82
  • 660
  • 783
0

Extract epoch millis from the original Date object and do integer division by 1000 followed by multiplication by 1000. Create Date object with the time zone of the original object and the millis calculated the above suggested way.

bobah
  • 18,364
  • 2
  • 37
  • 70
0

You can get the system time as follows without milliseconds

import java.text.SimpleDateFormat;
import java.util.Calendar;

And the code

   Calendar currentDate = Calendar.getInstance();
   SimpleDateFormat formatter= new SimpleDateFormat("dd-MM-YYYY-hh:mm:ss");
   String dateNow = formatter.format(currentDate.getTime());
   System.out.println(dateNow);
Anptk
  • 1,125
  • 2
  • 17
  • 28
0

if you want to mantain the format try something like that:

public static String getFechaTimestampToString (Timestamp timestamp) {
    String date = "";
    Calendar cal = Calendar.getInstance();  
    cal.setTime(new Date(timestamp.getTime()));  
    int year = cal.get(Calendar.YEAR);  
    int month = cal.get(Calendar.MONTH)+1;  
    int day = cal.get(Calendar.DAY_OF_MONTH);
    String monthstr = "";
    String daystr = "";

    if(month<10)
        monthstr = "0"+month;
    else
        monthstr = ""+month;

    if(day<10)
        daystr = "0"+day;
    else
        daystr = ""+day;

    date = year + "-" + monthstr + "-" + daystr ;

    return date;
}

To reverse data to database:

public static Timestamp getFechaStringToTimestamp (String strDate) {
    Timestamp timestamp = null;
    strDate = strDate + " 00:00:00";
    timestamp = Timestamp.valueOf(strDate);

    return timestamp;
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109