Calendar date to yyyy-MM-dd format in java

后端 未结 9 919
别跟我提以往
别跟我提以往 2020-11-22 06:16

How to convert calendar date to yyyy-MM-dd format.

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
Date date = cal.getTime();          


        
相关标签:
9条回答
  • 2020-11-22 06:37
    public static String ThisWeekStartDate(WebDriver driver) {
            Calendar c = Calendar.getInstance();
            //ensure the method works within current month
            c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            System.out.println("Before Start Date " + c.getTime());
            Date date = c.getTime();
    
              SimpleDateFormat dfDate = new SimpleDateFormat("dd MMM yyyy hh.mm a");
    
              String CurrentDate = dfDate.format(date);
              System.out.println("Start Date " + CurrentDate);
              return CurrentDate;
    
        }
        public static String ThisWeekEndDate(WebDriver driver) {
    
            Calendar c = Calendar.getInstance();
            //ensure the method works within current month
            c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            System.out.println("Before End Date " + c.getTime());
            Date date = c.getTime();
    
              SimpleDateFormat dfDate = new SimpleDateFormat("dd MMM yyyy hh.mm a");
    
              String CurrentDate = dfDate.format(date);
              System.out.println("End Date " + CurrentDate);
              return CurrentDate;
        }
    
    0 讨论(0)
  • 2020-11-22 06:38

    java.time

    The answer by MadProgrammer is correct, especially the tip about Joda-Time. The successor to Joda-Time is now built into Java 8 as the new java.time package. Here's example code in Java 8.

    When working with date-time (as opposed to local date), the time zone in critical. The day-of-month depends on the time zone. For example, the India time zone is +05:30 (five and a half hours ahead of UTC), while France is only one hour ahead. So a moment in a new day in India has one date while the same moment in France has “yesterday’s” date. Creating string output lacking any time zone or offset information is creating ambiguity. You asked for YYYY-MM-DD output so I provided, but I don't recommend it. Instead of ISO_LOCAL_DATE I would have used ISO_DATE to get this output: 2014-02-25+05:30

    ZoneId zoneId = ZoneId.of( "Asia/Kolkata" );
    ZonedDateTime zonedDateTime = ZonedDateTime.now( zoneId );
    
    DateTimeFormatter formatterOutput = DateTimeFormatter.ISO_LOCAL_DATE; // Caution: The "LOCAL" part means we are losing time zone information, creating ambiguity.
    String output = formatterOutput.format( zonedDateTime );
    

    Dump to console…

    System.out.println( "zonedDateTime: " + zonedDateTime );
    System.out.println( "output: " + output );
    

    When run…

    zonedDateTime: 2014-02-25T14:22:20.919+05:30[Asia/Kolkata]
    output: 2014-02-25
    

    Joda-Time

    Similar code using the Joda-Time library, the precursor to java.time.

    DateTimeZone zone = new DateTimeZone( "Asia/Kolkata" );
    DateTime dateTime = DateTime.now( zone );
    DateTimeFormatter formatter = ISODateTimeFormat.date();
    String output = formatter.print( dateTime );
    

    ISO 8601

    By the way, that format of your input string is a standard format, one of several handy date-time string formats defined by ISO 8601.

    Both Joda-Time and java.time use ISO 8601 formats by default when parsing and generating string representations of various date-time values.

    0 讨论(0)
  • 2020-11-22 06:39

    In order to parse a java.util.Date object you have to convert it to String first using your own format.

    inActiveDate = format1.parse(  format1.format(date)  );
    

    But I believe you are being redundant here.

    0 讨论(0)
  • 2020-11-22 06:40

    Your code is wrong. No point of parsing date and keep that as Date object.

    You can format the calender date object when you want to display and keep that as a string.

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 1);
    Date date = cal.getTime();             
    SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");          
    String inActiveDate = null;
    try {
        inActiveDate = format1.format(date);
        System.out.println(inActiveDate );
    } catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    
    0 讨论(0)
  • 2020-11-22 06:46
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month, date);
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy MM dd");
        String formatted = format1.format(cal.getTime());
        System.out.println(formatted);
    }
    
    0 讨论(0)
  • 2020-11-22 06:48
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 7);
    Date date = c.getTime();
    SimpleDateFormat ft = new SimpleDateFormat("MM-dd-YYYY");
    JOptionPane.showMessageDialog(null, ft.format(date));
    

    This will display your date + 7 days in month, day and year format in a JOption window pane.

    0 讨论(0)
提交回复
热议问题