Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting time from a Date Object

Tags:

java

date

time

I have an date object from which i need to getTime(). The issue is it always shows 00:00:00.

SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm:ss");
long date = Utils.getDateObject(DateObject).getTime();
String time = localDateFormat.format(date);

Why is the time always '00:00:00'. Should i append Time to my Date Object

like image 818
theJava Avatar asked May 16 '13 16:05

theJava


People also ask

How do you pass date and time in Date object?

We can create a date object by passing the Date object a timestamp number in milliseconds. For example, new Date(1572840117245) . When we create a date object by passing it 0 milliseconds, it will return the date object for Jan 01 1970 05:30:00 . This is the Linux Epoch date/time.

Does Date object have timezone?

Date objects most certainly DO have timezones. That's why they have two different sets of getters and setters. One for UTC, one for local.

Is there a time object in JavaScript?

The Date object is a built-in object in JavaScript that stores the date and time. It provides a number of built-in methods for formatting and managing that data. By default, a new Date instance without arguments provided creates an object corresponding to the current date and time.

How do I convert a date object to number?

Example: Convert Date to Number The new Date() gives the current date and time. To convert the name to a number, we use the getTime() method. The getTime() method returns the number of milliseconds from midnight of January 1, 1970 (EcmaScript epoch) to the specified date.


1 Answers

You should pass the actual Date object into format, not a long:

SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm:ss");
String time = localDateFormat.format(Utils.getDateObject(DateObject));

Assuming that whatever Utils.getDateObject(DateObject) is actually returns a Date (which is implied by your question but not actually stated), that should work fine.

For example, this works perfectly:

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

public class SDF {
    public static final void main(String[] args) {
        SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm:ss");
        String time = localDateFormat.format(new Date());
        System.out.println(time);
    }
}

Re your comment below:

Thanks TJ, but actually i am still getting 00:00:00 as time.

That means your Date object has zeroes for hours, minutes, and seconds, like so:

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

public class SDF {
    public static final void main(String[] args) {
        SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm:ss");
        String time = localDateFormat.format(new Date(2013, 4, 17)); // <== Only changed line (and using a deprecated API)
        System.out.println(time);
    }
}
like image 73
T.J. Crowder Avatar answered Nov 23 '22 17:11

T.J. Crowder