![]() String output = outputFormat.format(date) Complete Example : Convert Date from one format to another using SimpleDateFormat Here’s an example of how to format a date: This is done using the format() method of the SimpleDateFormat class. Step 3 : Formatting a Dateįinally, you can format the date and convert it to the desired output format. ![]() In this example, the date “12-06-2018” is parsed using the inputFormat object and stored in the “date” variable. Here’s an example of how to parse a date:ĭate date = inputFormat.parse("12-06-2018") This is done using the parse() method of the SimpleDateFormat class. The next step is to parse the input date and convert it to a Date object. In this example, the “inputFormat” object is set to the input format of “dd-MM-yyyy”, and the “outputFormat” object is set to the output format of “yyyy-MM-dd”. SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd") SimpleDateFormat inputFormat = new SimpleDateFormat("dd-MM-yyyy") Here’s an example of how to create a SimpleDateFormat object: You’ll use this object to specify the input format and the output format. The first step in converting dates is to create a “SimpleDateFormat” object. Converting Dates from one Format to Another in Java Step 1 : Creating a SimpleDateFormat Object In this post, we’ll look at how to use the “SimpleDateFormat” class to convert dates from one format to another. The exception is occurred due to the small hh in the input date value.In Java, you may need to convert dates from one format to another, whether it be for display purposes or to store the dates in a database. Now the following code will produce the expected results: public static void main(String args) throws ParseException ,ISO resolved to of type The output format can have small h or hh. Remember the above code will throw exception if you try to pass small h or hh in the time value as an input. String dtStr = outputDf.format(tempDateTime) LocalDateTime tempDateTime = LocalDateTime.parse(inputDateTime, inputDf) public static String convertDateTime8Format(String inputDateTime, String inputFormat, String outputFormat) The above code snippets formats only date value, if you want to change datetime value then you need to use LocalDateTime object. String dtStr = tempDate.format(outputDf) LocalDate tempDate = LocalDate.parse(inputDate, inputDf) In the above code I am using SimpleDateFormat to format the date in the desired format.įor Java 8 date type, you can use the following code snippets to change the input date format: public static String convertDate8Format(String inputDate, String inputFormat, String outputFormat)ĭateTimeFormatter inputDf = DateTimeFormatter.ofPattern(inputFormat) ĭateTimeFormatter outputDf = DateTimeFormatter.ofPattern(outputFormat) String dtStr = outputDf.format(tempDate) Here is the code snippets that convert input date to another format: public static String convertDateFormat(String inputDate, String inputFormat, String outputFormat)ĭateFormat inputDf = new SimpleDateFormat(inputFormat) ĭateFormat outputDf = new SimpleDateFormat(outputFormat) ĭate tempDate = inputDf.parse(inputDate) You may also want to convert string type date to Date object. The input date or datetime is string type and can be converted to another format. You can use prior to Java 8 and Java 8 onward ways to convert the date from one format to another format. There are many situations where you need to convert date format in your Java based application from one format to another format. In this example I am going to show you how to convert date from one format to another format in Java programming language.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |