Display the Current Time and Date in an Android Application

Display the current time and date in an Android application

Okay, not that hard as there are several methods to do this. I assume you want to put the current date & time into a TextView.

String currentDateTimeString = java.text.DateFormat.getDateTimeInstance().format(new Date());

// textView is the TextView view that should display it
textView.setText(currentDateTimeString);

There is more to read in the documentation that can easily be found here
. There you'll find more information on how to change the format used for conversion.

How to get current time and date in Android

You could use:

import java.util.Calendar

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

There are plenty of constants in Calendar for everything you need.

Check the Calendar class documentation.

Showing current time in Android and updating it?

Something like this should do the trick

final Handler someHandler = new Handler(getMainLooper());   
someHandler.postDelayed(new Runnable() {
@Override
public void run() {
tvClock.setText(new SimpleDateFormat("HH:mm", Locale.US).format(new Date()));
someHandler.postDelayed(this, 1000);
}
}, 10);

You should keep a reference to the handler and the runnable to cancel this when the Activity goes to pause and resume when it resumes. Make sure you remove all callbacks to handler and set it to null in onDestroy

How can I get current date in Android?

You can use the SimpleDateFormat class for formatting date in your desired format.

Just check this link where you get an idea for your example.

For example:

String dateStr = "04/05/2010"; 

SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
Date dateObj = curFormater.parse(dateStr);
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");

String newDateStr = postFormater.format(dateObj);

Update:

The detailed example is here, I would suggest you go through this example and understand the concept of SimpleDateFormat class.

Final Solution:

Date c = Calendar.getInstance().getTime();
System.out.println("Current time => " + c);

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault());
String formattedDate = df.format(c);

How to get current date and time in Android?


    Calendar calander = Calendar.getInstance(); 
cDay = calander.get(Calendar.DAY_OF_MONTH);
cMonth = calander.get(Calendar.MONTH) + 1;
cYear = calander.get(Calendar.YEAR);
selectedMonth = "" + cMonth;
selectedYear = "" + cYear;
cHour = calander.get(Calendar.HOUR);
cMinute = calander.get(Calendar.MINUTE);
cSecond = calander.get(Calendar.SECOND);

How to access Date and time using Android

You can parse whatever you want just change values inside SimpleDateFormat.

SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
System.out.println(format.format(new Date()));

If you want just time

SimpleDateFormat format = new SimpleDateFormat("hh:MM:ss");
System.out.println(format.format(new Date()));

Or you can store this datetime in string

DateFormat df = new SimpleDateFormat("HH:mm:ss");

// Get the date
Date today = Calendar.getInstance().getTime();
// Here you can create a string
String reportDate = df.format(today);

// For example print date or do what ever you like to do with it
System.out.println("Report Date: " + reportDate);

How to get the current time in android


Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("HH:mm a");
// you can get seconds by adding "...:ss" to it
date.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));

String localTime = date.format(currentLocalTime);

How can i get current date and time in android and store it into string


Update 01/Sept/2020 - Sample Kotlin DateUtilExtensions using Java SE 8

@file:JvmName("DateUtilExtensions")

package com.intigral.jawwytv.util.extensions

import android.text.TextUtils
import android.text.format.DateUtils
import java.text.ParseException
import java.text.SimpleDateFormat
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.util.Date
import java.util.Locale
import java.util.TimeZone

const val PATTERN_YEAR = "yyyy"
const val PATTERN_MONTH = "MMM"
const val PATTERN_MONTH_FULL = "MMMM"
const val PATTERN_DAY_OF_MONTH = "dd"
const val PATTERN_DAY_OF_WEEK = "EEEE"
const val PATTERN_TIME = "hh:mm a"
const val PATTERN_TIME_24H = "HH:mm"
const val PATTERN_SERVER_DATE = "yyyy-MM-dd"
const val PATTERN_SERVER_DATE_TIME = "yyyy-MM-dd HH:mm:ss"
const val PATTERN_START_WITH_MONTH = "MMM dd , yyyy"
const val PATTERN_START_WITH_MONTH_NO_YEAR = "MMMM dd"
const val PATTERN_START_WITH_DATE_NO_YEAR = "dd MMMM"
const val PATTERN_START_WITH_MONTH_SHORT_NO_YEAR = "MMM dd"
const val PATTERN_START_WITH_MONTH_WITH_TIME = "MMM dd, yyyy HH:mm:ss"
const val PATTERN_START_WITH_MONTH_SMALL_NO_YEAR = "MMM dd"

fun formatDate(pattern: String): String {
val localDateTime = LocalDateTime.now()
return localDateTime.format(DateTimeFormatter.ofPattern(pattern))
}

fun formatDate(localDateTime: LocalDateTime, pattern: String): String =
localDateTime.format(DateTimeFormatter.ofPattern(pattern))

fun formatDate(timeInMills: Long?, pattern: String): String =
LocalDateTime.ofInstant(Instant.ofEpochMilli(timeInMills ?: 0), ZoneId.systemDefault())
.format(DateTimeFormatter.ofPattern(pattern))

fun todayToEpochMilli() =
LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()

fun isDateEqual(dateInMills: Long?, dateInMillsOther: Long?): Boolean {
val systemDefault = ZoneOffset.systemDefault()
val date = Instant.ofEpochMilli(dateInMills ?: 0).atZone(systemDefault).toLocalDate()
val otherDate =
Instant.ofEpochMilli(dateInMillsOther ?: 0).atZone(systemDefault).toLocalDate()
return date.isEqual(otherDate)
}

fun convertDateString(inputPattern: String, outputPattern: String, stringDate: String): String? {
val originalFormat = SimpleDateFormat(inputPattern, Locale.getDefault())
val targetFormat = SimpleDateFormat(outputPattern, Locale.getDefault())
val requiredFormat = originalFormat.parse(stringDate)
return requiredFormat?.let { targetFormat.format(requiredFormat) }
}

fun getRelativeTimeSpanString(
dateString: String,
format: String = PATTERN_SERVER_DATE_TIME
): String? {
if (!TextUtils.isEmpty(dateString)) {
val simpleDateFormat = SimpleDateFormat(format, Locale.getDefault())
simpleDateFormat.timeZone = TimeZone.getTimeZone(ZoneOffset.UTC)
var date: Date? = null
try {
date = simpleDateFormat.parse(dateString)
} catch (e: ParseException) {
e.printStackTrace()
}

val epochTime = date!!.time

val relTime = DateUtils.getRelativeTimeSpanString(
epochTime,
System.currentTimeMillis(),
DateUtils.SECOND_IN_MILLIS
)

return relTime.toString()
}
return dateString
}

Initial Answer - use SimpleDateFormat

Date today = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
String dateToStr = format.format(today);
System.out.println(dateToStr);

How to get current time and date in Android

You could use:

import java.util.Calendar

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

There are plenty of constants in Calendar for everything you need.

Check the Calendar class documentation.



Related Topics



Leave a reply



Submit