Home Tutorial Java Core Java add milliseconds to Date

 
 

Java add milliseconds to Date
Posted on: October 11, 2012 at 12:00 AM
Advertisement
In this tutorial, you will learn how to add milliseconds to date.

Java add milliseconds to Date

In this tutorial, you will learn how to add milliseconds to date.

Java Calendar class is a very useful and handy class. It is basically used in date time manipulation. Here, we are going to add milli-seconds to current date and return the resultant time. For this, we have created a calendar instance and get a date to represent the current date. Then using the method add() of Calendar class, we have added 186 milliseconds to the calendar which in result display the resulted time.

Example:

import java.util.*;
import java.text.*;

public class AddMilliSecondsToDate{

  public static void main(String[] args){
    Calendar calendar = Calendar.getInstance();
    Date today = calendar.getTime();
	SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss S");
    System.out.println("Current Time: " + sdf.format(today));
    calendar.add(Calendar.MILLISECOND, 186);
    Date addMilliSeconds = calendar.getTime();
    System.out.println("Time after 186 milliseconds: " + sdf.format(addMilliSeconds));
  }
}

Output:

Current Time: 12:59:28 968
Time after 186 milliseconds: 12:59:29 154
Advertisement

Related Tags for Java add milliseconds to Date:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 11, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com