![]() ![]() Here, String type date is converted into the Date using formatter.parse() method. Date date = formatter.parse(date_String) This is the simple date given in the string format. For further clarifications, you can refer the table given above. So, this is the format of formatted which can be applied to any date later. In the above example "dd-MMM-yyyy" can be further simplified like this: SimpleDateFormat is a class from java.text package which sets the format of a date. Now let's break the above code and understand line by line SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy") ![]() SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy") ĭate date = formatter.parse(date_String) While converting strings to date makes sure that it is in the correct format otherwise it will throw an exception i.e. In this example, we are converting the given date which is in string format to Date by format using SimpleDateFormat class. All Available Formats of Date and Time in Java Java supports many formats to customize dates with a lot of flexibility. This means the mentioned date is in the format of “dd/MM/yyyy”. In the code, you might have noticed SimpleDateFormat("dd/MM/yyyy") method to set the format of a date. While executing the above example do not forget to use the throws keyword in the main method otherwise compile-time error will occur. (" Date in String Format: "+string_format+" \n Converted Date: "+date_format) ĭate in String Format: Converted Date: Mon Jan 04 00:00: Public static void main(String args) throws Exceptionĭate date_format=new SimpleDateFormat("dd/MM/yyyy").parse(string_format) Try this example on your own, it will help you to get the intuition behind this process of converting strings to date. Here is a very classic example of converting String to Date. By storing dates in plain text we can save a lot of storage space. It is a very common practice in the software industry to store dates in plain text format and use it later for business logic. In this article, we will walk you through very simple and understandable ways of converting Strings to dates in Java.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |