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.
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);
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 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 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 get current local date and time in Kotlin
java.util.Calendar.getInstance()
represents the current time using the current locale and timezone.
You could also choose to import and use Joda-Time or one of the forks for Android.
Get current time only and update it in android
You just need to have a final variable SimpleDateFormat
.
final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
and you can get time with format in anywhere you want using this code:
String time = dateFormat.format(new Date());
Related Topics
How to Get the Android Device'S Primary E-Mail Address
How to Use Getdownloadurl in Recent Versions
Android How to Adjust Layout in Full Screen Mode When Softkeyboard Is Visible
Android Min Sdk Version Vs. Target Sdk Version
How to Get the External Sd Card Path For Android 4.0+
Android Fragment No View Found For Id
Install_Failed_No_Matching_Abis When Install Apk
How to Hook into the Power Button in Android
How to Change an Application Icon Programmatically in Android
Example: Communication Between Activity and Service Using Messaging
How to Handle Button Clicks Using the Xml Onclick Within Fragments
How to Call Android Contacts List
Understanding Fragment'S Setretaininstance(Boolean)
Onsaveinstancestate () and Onrestoreinstancestate ()