1

I was building an android app and i was using Date class in my project to get the current date. I formatted the date with simpledateformatter and displayed it like dd-mm-yyyy (i.e. day month year) .

Now i also want to get the time in format of hh:MM:ss a (hours minutes seconds AM/PM)

As i was using date's instance i saw that it displays date and time also ( in default format). So i tried to fetch time from the date's instance.(let's say d is date class instance). I also found getTime() method of date class and performed d.getTime() but it returned me a long (which is duration from some fixed time from past to current time). Now i want time in desired format but this getTime() method is giving me long.

May you provide me some way on how to process this long value to get the desired format of time out of it. For example , d.getTime() return me some value( say 11233) and i want in format like this (11:33:22).

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Arjun Sharma
  • 101
  • 2
  • 2
  • 10

3 Answers3

2

You can make that

private final String DATE_FORMAT = "dd.MM.yyyy HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
Date got = sdf.parse(date);

It returns Date with time to you

Dred
  • 1,076
  • 8
  • 24
  • But i want to extract time out of it and not the date , then should i do? – Arjun Sharma Jul 04 '19 at 12:25
  • Although i can make it string and can extract time out of it. But , will it be a good Decision to extract it like that? – Arjun Sharma Jul 04 '19 at 12:25
  • @ArjunSharma Do you need `Time` or `String`? Or which Type of your time do you want to get? – Dred Jul 04 '19 at 12:31
  • I am sorry i am little bit confused! I want to know a thing first, i have a button which displays the time in it. And when i press that button then it shows timepicker dialog and displays the time which was previously set. And now if some change time i would like to store it and display it on the time button. Which type i should use for the time? – Arjun Sharma Jul 04 '19 at 14:54
2

Use this snippet to get the date and time both.

public String currentDateTime() {
    Calendar c = Calendar.getInstance();
    SimpleDateFormat dateformat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss aa");  //it will give you the date in the formate that is given in the image
    String datetime = dateformat.format(c.getTime()); // it will give you the date
    return datetime;
}

Note: Take a look in the image enter image description here.

Shahadat Hossain
  • 533
  • 7
  • 20
  • And if i need only time out of it then i can make it a string then extract the time as a substring out of it , right? – Arjun Sharma Jul 04 '19 at 12:29
1

Date().getTime() is providing you the timestamp

Change the format to your requirement like mm:hh:ss a

Kotlin

fun getDateTime():String {
    val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
    val date = Date()
    return inputFormat.format(date.time)
}

JAVA

private String getDateTime(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
        return format.format(new Date().getTime());
    }
Lakhwinder Singh
  • 6,799
  • 4
  • 25
  • 42