How to Get Current Date in Android

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 time and date in Android

You could use:

import java.util.Calendar;
import java.util.Date;

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

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

Check the Calendar class documentation.

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 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 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.

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);

Get current date in YYYY-MM-DD format in Java (Android)

Change this:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");

to this:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

mm stands for the minutes, while MM (capitalized) stands for the month

Show CurrentDate In TextView

Try this

import java.time.format.DateTimeFormatter;  
import java.time.LocalDateTime;
public class Main{
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy.MM.dd");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now));
}

}

for android studio

dateView1.setText(dtf.format(now));

Same theme -> https://stackoverflow.com/a/5369753/16300574



Related Topics



Leave a reply



Submit