How to convert a date in a string formatted as "yyyy-MM-dd" to date in Java

1 Answer

0 votes
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
  
public class JavaApplication {
    public static void main(String[] args) {
        String sDate = "2024-04-28";
 
        try {
            SimpleDateFormat sdfFrom = new SimpleDateFormat("yyyy-MM-dd");
  
            Date date = sdfFrom.parse(sDate);
 
            System.out.println(date);
             
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
             
            System.out.println(sdf.format(date));
  
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
    }  
}
 
 
     
/*
     
run:
     
Sun Apr 28 00:00:00 GMT 2024
2024-04-28
     
*/

 



answered Jun 13, 2024 by avibootz
edited Jun 13, 2024 by avibootz

Related questions

1 answer 150 views
1 answer 141 views
1 answer 171 views
1 answer 90 views
1 answer 87 views
...