I am trying to parse the current time and date into this simple date format.
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy-kk:mm");
Date datetoday = sdf.parse(Calendar.getInstance().getTime().toString());
This is how you obtain current date represented as a Java Date object:
Date date = new Date();
This is how you obtain current date as a formatted string:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy-kk:mm"); String formattedDateString = sdf.format(new Date());
You can then further parse it back into a Date:
Date date = sdf.parse(formattedDateString);
But there is no point in doing that because you already had the Date when you did the .format(...) call, only in your case you used
which typically gives the same result as
If you want to parse a string into a Date then the string needs to match the format specified in SimpleDateFormat constructor. Date's toString() method that you are using returns a string in a specific format that does not match the format from your example, that's why you are getting the ParseException error.