-1

I'am trying to get current time and make it textview. What i find is 10 or 12 years old and i dont realy know if thats working and how it works. I saw that you can use firebase and another sites or apps for it. I'd like to get some help with it.

Link

Some code i find

Date currentTime = Calendar.getInstance().getTime();

Calendar.getInstance().get(Calendar.HOUR_OF_DAY);

Calendar.getInstance().get(Calendar.MINUTE);

Thanks all.

  • what is make it textview? what do you want to do? – mmdreza baqalpour Apr 12 '22 at 08:22
  • Check following : `long date = System.currentTimeMillis();` `SimpleDateFormat sdf = new SimpleDateFormat("MMM MM dd, yyyy h:mm a");` `String dateString = sdf.format(date);` `tvDisplayDate.setText(dateString);` This should display in the following example format `Tue Apr 12, 2022 10:26 AM` tvDisplayDate is text view . For different formats of date time you can refer following link : [Oracle](https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html) – Sarabjeet Singh Apr 12 '22 at 08:25

1 Answers1

1

For the current Time only

long currentDateTime = System.currentTimeMillis();
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mm a");
        String currentTime = simpleDateFormat.format(currentDateTime);
        Toast.makeText(this, currentTime, Toast.LENGTH_SHORT).show();

and if you want the Date & Time both then the below code is for you

        long currentDateTime = System.currentTimeMillis();
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy  h:mm a");
        String currentDateTime = simpleDateFormat.format(currentDateTime);
        Toast.makeText(this, currentDateTime, Toast.LENGTH_SHORT).show();

EDIT 1 another method to do this

LocalDateTime myDt= LocalDateTime.now();
        Toast.makeText(this, myDt.getHour()+":"+myDt.getMinute()+":"+myDt.getSecond(), Toast.LENGTH_SHORT).show();

EDIT 2 another method to get Date only

LocalDateTime myDt= LocalDateTime.now();
        DateTimeFormatter newDt = DateTimeFormatter.ISO_DATE;
        String dates = newDt.format(myDt);
        Toast.makeText(this,dates, Toast.LENGTH_LONG).show();

You can get more options to get a date, time, GMT, or more by changing from ISO_DATE to ISO_DATE_TIME, ISO_LOCAL_DATE_TIME etc.

M DEV
  • 763
  • 1
  • 7
  • 20
  • I have 2 problems with SimpleDateFormat and format this two are red for me. On simpleDateFormat i have "Cannot resolve symbol 'SimpleDateFormat' " error and on format i have "Cannot resolve method 'format(long)' ". Maybe i need any implementation in gradle? –  Apr 12 '22 at 08:56
  • @DaniilNedbaylo Ok, use this method-> `LocalDateTime myDt= LocalDateTime.now(); Toast.makeText(this, myDt.getHour()+":"+myDt.getMinute()+":"+myDt.getSecond(), Toast.LENGTH_SHORT).show(); System.out.println(myDt.getHour()+":"+myDt.getMinute()+":"+myDt.getSecond());` – M DEV Apr 12 '22 at 09:15
  • Thanks thats works do you have any idea for date? –  Apr 12 '22 at 09:22
  • @DaniilNedbaylo yah why not. I added the date code in my answer. – M DEV Apr 12 '22 at 10:11