Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is DateUtils.setMilliseconds() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

setMilliseconds() is a staticthe methods in Java that can be called without creating an object of the class. method of the DateUtils class that sets the millisecond field of the Date object returning a new Date object. The original Date object remains unchanged.

How to import DateUtils

The definition of DateUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the DateUtils class as follows:


import org.apache.commons.lang3.time.DateUtils;

Syntax


public static Date setMilliseconds(final Date date, final int amount)

Parameters

  • final Date date: The original Date object.
  • final int amount: The new value of millisecond to set.

Return value

This method returns a new Date object with the millisecond field set to the specified value.

Code

import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main{

    public static void main(String[] args) {
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy HH:mm:ss:SS");
        int newMilliSecondValue = 999;
        Date newDate = DateUtils.setMilliseconds(date, newMilliSecondValue);
        System.out.printf("DateUtils.setMilliseconds(%s, %s) = %s", dateFormat.format(date), newMilliSecondValue, dateFormat.format(newDate));
    }
}

Output

The output of the code will be as follows:


DateUtils.setMilliseconds(21/11/21 13:55:06:177, 999) = 21/11/21 13:55:06:999

Explanation

  • From lines 1 to 5, we import the relevant packages.
  • In line 7, we create a Main class.
  • In line 9, we make a main() function.
  • In line 10, we create a Date object.
  • In line 11, we create a SimpleDateFormat object with the format in which we want the dates to be printed.
  • In line 12, we define the value of milliseconds to set.
  • In line 13, we use the setMilliseconds() method to set the milliseconds field of the date object defined in line 10.
  • In line 14, we print the original date, milliseconds value, and the new date object we obtained in line 13.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring