Skip to main content

Posts

Showing posts with the label simpledateformat

Converting String to Date and Date to String in android

I struggled almost for an entire day trying to access and manipulate dates for my app. If I use date class, many of the methods show warning that they are deprecated. I took the help of simple date format and tried showing month and date. But some how the output was always wrong.  So, if you need to access dates, set a date to current date or to any other particular date and also read dates from a edit text, you should ideally use 3 classes  viz Calendar, Date and SimpleDateFormat. First create a Calendar object and initialize it with current date.        Calendar cal  = Calendar.getInstance();    Now to convert this to Date object use getTime() method.      Date someDate = cal.getTime();//this returns a date object. Now to get a string representation of this date use SimpleDateFormat class             SimpleDateFormat dtFormat = new SimpleDateFormat("dd-MMM...