Ask Your Question
1

Can the code "timestamp = System.currentTimeMillis()" be changed to display the date and time in a specific format such as "DD-MM-YYYY HH:MM:SS"?

asked 2023-01-13 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-05-20 06:00:00 +0000

devzero gravatar image

Yes, the code can be changed to display the date and time in a specific format using the SimpleDateFormat class. Here's an example:

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

public class TimestampExample 
{
    public static void main(String[] args) 
    {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        String dateAndTime = sdf.format(new Date(System.currentTimeMillis()));
        System.out.println(dateAndTime);
    }
}

This code creates a SimpleDateFormat object with the desired date and time format, then formats the current date and time as a string using the format() method. Finally, it prints the date and time string to the console.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-01-13 11:00:00 +0000

Seen: 7 times

Last updated: May 20 '22