How to convert date in string from one format to another format 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 = "29/10/16";
 
        try {
            SimpleDateFormat sdfFrom = new SimpleDateFormat("dd/MM/yy");
 
            Date date = sdfFrom.parse(sDate);
 
            SimpleDateFormat sdfTo = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
 
            sDate = sdfTo.format(date);
 
            System.out.println(sDate);
 
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
    }  
}


    
/*
    
run:
    
10-29-2016 12:00:00
    
*/

 



answered Oct 29, 2016 by avibootz
edited Jun 13, 2024 by avibootz

Related questions

1 answer 111 views
1 answer 153 views
1 answer 152 views
1 answer 112 views
1 answer 108 views
1 answer 103 views
1 answer 278 views
...