Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Date to a particular format in android?

Tags:

java

android

"Mar 10, 2016 6:30:00 PM" This is my date and I want to convert this into "10 Mar 2016". Can I use SimpleDateFormat in android. I am not getting the exact pattern to convert it. Please help and thanks in advance

String date="Mar 10, 2016 6:30:00 PM";
SimpleDateFormat spf=new SimpleDateFormat("Some Pattern for above date");
Date newDate=spf.format(date);
spf= new SimpleDateFormat("dd MMM yyyy");
String date = spf.format(newDate);

Will this steps work? If yes, can someone please give me a pattern of that format? Thanks in advance.

like image 883
Rahul Verma Avatar asked Mar 11 '16 11:03

Rahul Verma


People also ask

How can I change the date format in Android?

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy hh:mm a"); String date = formatter. format(Date. parse("Your date string")); Hope it will help you.

How do I format a simple date?

For example, using a pattern of "MM/dd/yy" and a SimpleDateFormat instance created on Jan 1, 1997, the string "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64" would be interpreted as May 4, 1964. During parsing, only strings consisting of exactly two digits, as defined by Character.

What is date format in Android Studio?

DateFormat. format("yyyy-MM-dd hh:mm:ss a", new java.


4 Answers

This is modified code that you should use:

String date="Mar 10, 2016 6:30:00 PM"; SimpleDateFormat spf=new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aaa"); Date newDate=spf.parse(date); spf= new SimpleDateFormat("dd MMM yyyy"); date = spf.format(newDate); System.out.println(date); 

Use hh for hours in order to get correct time.

Java 8 and later

Java 8 introduced new classes for time manipulation, so use following code in such cases:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy h:mm:ss a");     LocalDateTime dateTime = LocalDateTime.parse(date, formatter);     DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd MMM yyyy");     System.out.println(dateTime.format(formatter2)); 

Use h for hour format, since in this case hour has only one digit.

like image 54
user987339 Avatar answered Sep 30 '22 01:09

user987339


You can use following method for this problem. We simply need to pass Current date format, required date format and Date String.

private String changeDateFormat(String currentFormat,String requiredFormat,String dateString){     String result="";     if (Strings.isNullOrEmpty(dateString)){         return result;     }     SimpleDateFormat formatterOld = new SimpleDateFormat(currentFormat, Locale.getDefault());     SimpleDateFormat formatterNew = new SimpleDateFormat(requiredFormat, Locale.getDefault());     Date date=null;     try {         date = formatterOld.parse(dateString);     } catch (ParseException e) {         e.printStackTrace();     }     if (date != null) {         result = formatterNew.format(date);     }     return result; } 

This method will return Date String in format you require. In your case method call will be:

String date = changeDateFormat("MMM dd, yyyy hh:mm:ss a","dd MMM yyyy","Mar 10, 2016 6:30:00 PM"); 
like image 23
Rahul Sharma Avatar answered Sep 30 '22 00:09

Rahul Sharma


conversion from string to date and date to string

String deliveryDate="2018-09-04";                       
SimpleDateFormat dateFormatprev = new SimpleDateFormat("yyyy-MM-dd");
Date d = dateFormatprev.parse(deliveryDate);
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE dd MMM yyyy");
String changedDate = dateFormat.format(d);
like image 36
Syed Danish Haider Avatar answered Sep 30 '22 00:09

Syed Danish Haider


You should parse() the String into Date and then format it into the desired format. You can use MMM dd, yyyy HH:mm:ss a format to parse the given String.

Here is the code snippet:

public static void main (String[] args) throws Exception
{
    String date = "Mar 10, 2016 6:30:00 PM";
    SimpleDateFormat spf = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a");
    Date newDate = spf.parse(date);
    spf = new SimpleDateFormat("dd MMM yyyy");
    String newDateString = spf.format(newDate);
    System.out.println(newDateString);
}

Output:

10 Mar 2016
like image 22
user2004685 Avatar answered Sep 30 '22 00:09

user2004685