Home > Mobile >  Current time into textview
Current time into textview

Time:04-12

All i want is 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.

CodePudding user response:

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.

  • Related