Search code examples
androidjodatimeandroid-jodatime

How to get only date type without time?


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)

Solution

  • 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!