How to get Date object in “YYYY-MM-DD” format in android

后端 未结 5 1532
借酒劲吻你
借酒劲吻你 2021-02-18 20:04

I have a requirement that I need to compare two Dates. One Date will come from DB which is String in \"YYYY-DD-MM\" firm and I need to compare this String Date with current Date

相关标签:
5条回答
  • 2021-02-18 20:27

    You can do it in following way

    // pick current system date
    
        Date dt = new Date();
    
    // set format for date
    
       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    
     // parse it like
    
            String check = dateFormat.format(dt);
    
    
            System.out.println("DATE TO FROM DATEBASE   " + 
                    arrayOfStringDate[d].toString());
            System.out.println("CURRENT DATE   " + check);
    

    // and compare like

    System.out.println("compare    "+ 
            arrayOfStringDate[d].toString().equals(check));
    
    0 讨论(0)
  • 2021-02-18 20:31
    Calendar c = Calendar.getInstance();
    SimpleDateFormat tf = new SimpleDateFormat("yyyy-MM-dd");
    String time=DB time;
    Date parseTime= tf.parse(time);
    Integer dayNow=c.getTime().getDate();
    Integer dayDb=parseTime.getDate();
    

    then you can compare dayNow and dayDb.

    0 讨论(0)
  • 2021-02-18 20:36
     Date cDate = new Date();
     String fDate = new SimpleDateFormat("yyyy-MM-dd").format(cDate);
    
    0 讨论(0)
  • 2021-02-18 20:41

    You can use 2 ways:

    1. DateFormat object. Use parse method.

    2. Make your own parser of the Date. I mean, you convert the year, month and day in an integer each, and use Date constructor to get the Date.

    0 讨论(0)
  • 2021-02-18 20:49

    If your current date is actually an instance of the java.util.Date class, you don't need to specify a format for it; it's just a millisecond value that represents a specific moment in time.

    You can get the current date like so:

    Date currentDate = new Date();
    
    0 讨论(0)
提交回复
热议问题