In my android project I need to go through data base and take some values from a current record, but only if this record has the date I need.
I tried googling it and i dont think there is a good enough explanation
Save the current date in string form such as '08/02/2023' then convert it into date form through sdf(SimpleDateFormat)
val sdf = SimpleDateFormat("dd/MM/yyyy")
var date: String = "08/02/2023" //To be converted; in string form
val dateConverted: Date = sdf.parse(date) // Converted; in date form
The next step is to convert both the dates to be compared in to Date form and then compare through the if statement;
Example:
val date1: Date = <Your first date in date form>
val date2: Date = <Your second date in date form>
if(date1 == date2){
// Do something
} else {
// Do something
}
Conclusion:
for(date in <your date array>){
val sdf = SimpleDateFormat("dd/MM/yyyy")
val date: Date = sdf.parse(date) // Converted; in date form
if(date == <date you want to convert in Date form>){
// Do something
} else {
// Do something
}
}
This solution might help you.
Thank you
Related
I am taking the current date in ymd format as below :
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
I also have a date as String as below :
String futureDate = "2022-03-07"
How can I know if the current date is less than or greater than future date ?
I tried to use compareTo() function, but was unable to compare the two dates.
To compare dates you can convert to Date and use:
final Date other = simpleDateFormat.parse("2022-03-07");
final Date now = new Date();
if (other.after(now)) {
// In the future!
} else if(other.before(now)) {
// In the past!
} else {
// In the present!
}
I have a simple code where i want to convert a certain given time to another time zone , in my case the time is local to UK , but i need to convert the time to another timezone if the user lives in different country , i have tried this simple code but it is not working for me , it is giving me random hour 04:00
any help would be appreciated guys
This is the code
var localTime = "16:00" // simulating time to Uk timezone
localtime.text = localTime
timeZone.setOnClickListener {
val localTimes = "16:00"
val timeFormatter = SimpleDateFormat("hh:mm", Locale.UK)
val timezone = TimeZone.getDefault() // get device timezone
timeFormatter.timeZone = timezone
val timeToFormat = timeFormatter.parse(localTimes)
val formattedTime = timeFormatter.format(timeToFormat)
localtime.text = formattedTime
}
I am trying to parse the following UTC string into a date in the central timezone.
date = "2019-11-18T16:00:00.000Z"
This code works and parses the date string into a date object with the timezone applied:
private fun getTimeZoneDate(date: String): Date {
val parser = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
parser.timeZone = TimeZone.getTimeZone("CST")
return parser.parse(date)
}
However, I need to use the zone ID as it is what my API returns. This does not work:
private fun getTimeZoneDate(date: String): Date {
val parser = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
parser.timeZone = TimeZone.getTimeZone("America/Chicago")
return parser.parse(date)
}
What is the different? Why doesn't the second chunk of code do anything to the string date but the first chunk (with timezone set to "CST") work?
I want to use Date type as Id in Room database, The main reason for this is to be able to check if this date is today. Any suggestion is appreciate
#Entity
data class DailyCount(#PrimaryKey
var date:DateTime,// JodaTime
var summ: MutableLiveData<Double>? = MutableLiveData())
I want to make query like this:
#Query("update DailyCount set summ = :sum where date = :dailyCount") //update if date is today
fun apdateCash(dailyCount: DateTime, sum: Double)
Just use SimpleDateFormatter
String jsonDateStr = "03-09-2019 05:45:10"
SimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy");
try {
return fmt.parse(jsonDateStr);
} catch(ParseException pe) {
return //generate different unique ID like GUID random maybe;
}
Now if you have a date object first, and you need to zero it out, you could do:
Date dateObject = Date("03-09-2019 05:45:10") //pseudo for visual
SimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy");
try {
String dateWithZeroedTime = fmt.format(dateObject)
return fmt.parse(dateWithZeroedTime) //"03-09-2019 00:00:00"
} catch(ParseException pe) {
return //generate different unique ID like GUID random maybe;
}
Happy Coding!
I'm learning how to build Android applications, and I'm trying to get the age from my users, only using the birthday.
I'm already using Joda timer, but I'm getting the data from a Json file, and this Json file outputs the data like this:
1994-11-24 / YYYY-MM-d
I'm getting the json data inside a for loop, in Java.
//Variable
private static final String TAG_BIRTH_DATE = "birth_date";
...
//inside the Loop
String birth_date = c.getString(TAG_BIRTH_DATE);
My question is, how can I format the date, and get the age from the person?
I tried this, so far.
DateTimeFormatter formatter = DateTimeFormat.forPattern("d/MM/yyyy");
LocalDate date = formatter.parseLocalDate(birth_date);
LocalDate birthdate = new LocalDate (date);
LocalDate now = new LocalDate();
Years age = Years.yearsBetween(birthdate, now);
But is not working.
Thank you.
Try below method to calculate age of user and in parameter pass your date that you are getting from JSON
public static int getAge(String dateOfBirth) {
Calendar today = Calendar.getInstance();
Calendar birthDate = Calendar.getInstance();
int age = 0;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateOfBirth);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
birthDate.setTime(convertedDate);
if (birthDate.after(today)) {
throw new IllegalArgumentException("Can't be born in the future");
}
age = today.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);
// If birth date is greater than todays date (after 2 days adjustment of
// leap year) then decrement age one year
if ((birthDate.get(Calendar.DAY_OF_YEAR)
- today.get(Calendar.DAY_OF_YEAR) > 3)
|| (birthDate.get(Calendar.MONTH) > today.get(Calendar.MONTH))) {
age--;
// If birth date and todays date are of same month and birth day of
// month is greater than todays day of month then decrement age
} else if ((birthDate.get(Calendar.MONTH) == today.get(Calendar.MONTH))
&& (birthDate.get(Calendar.DAY_OF_MONTH) > today
.get(Calendar.DAY_OF_MONTH))) {
age--;
}
return age;
}
You have just mismatched your pattern and then accepted an answer which uses minutes instead of months (small "m" versus big "M). The Joda-answer would be (pay attention to the different pattern please):
String birth_date = c.getString(TAG_BIRTH_DATE); // example: 1994-11-24
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-d");
LocalDate date = formatter.parseLocalDate(birth_date);
Years age = Years.yearsBetween(date, LocalDate.now());