Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How can I Convert String to Date?

People also ask

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.

Which function is used convert string into date format?

Date() Function. as. Date() function in R Language is used to convert a string into date format.

How do I change a string to a date in Kotlin?

Example 2: Convert String to Date using pattern formatters So, we create a formatter of the given pattern. Check all DateTimeFormatter patterns, if you're interested. Now, we can parse the date using LocalDate. parse() function and get the LocalDate object.

How can I get current date in Android?

getInstance(). getTime(); System. out. println("Current time => " + c); SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy"); String formattedDate = df.


From String to Date

String dtStart = "2010-10-15T09:27:37Z";  
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
try {  
    Date date = format.parse(dtStart);  
    System.out.println(date);  
} catch (ParseException e) {
    e.printStackTrace();  
}

From Date to String

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
try {  
    Date date = new Date();  
    String dateTime = dateFormat.format(date);
    System.out.println("Current Date Time : " + dateTime); 
} catch (ParseException e) {
    e.printStackTrace();  
}

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date d = dateFormat.parse(datestring)

     import java.text.ParseException;
     import java.text.SimpleDateFormat;
     import java.util.Date;
     public class MyClass 
     {
     public static void main(String args[]) 
     {
     SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");

     String dateInString = "Wed Mar 14 15:30:00 EET 2018";

     SimpleDateFormat formatterOut = new SimpleDateFormat("dd MMM yyyy");


     try {

        Date date = formatter.parse(dateInString);
        System.out.println(date);
        System.out.println(formatterOut.format(date));

         } catch (ParseException e) {
        e.printStackTrace();
         }
    }
    }

here is your Date object date and the output is :

Wed Mar 14 13:30:00 UTC 2018

14 Mar 2018


using SimpleDateFormat or DateFormat class through

for e.g.

try{
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); // here set the pattern as you date in string was containing like date/month/year
Date d = sdf.parse("20/12/2011");
}catch(ParseException ex){
    // handle parsing exception if date string was different from the pattern applying into the SimpleDateFormat contructor
}

You can use java.time in Android now, either by using Android API Desugaring or importing the ThreeTenAbp.

With java.time enabled, you can do the same operations with less code and less errors.

Let's assume you are passing a String containing a datetime formatted in ISO standard, just as the currently accepted answer does.
Then the following methods and their usage in a main may show you how to convert from and to String:

public static void main(String[] args) {
    String dtStart = "2010-10-15T09:27:37Z";
    ZonedDateTime odt = convert(dtStart);
    System.out.println(odt);
}

and

public static void main(String[] args) {
    String dtStart = "2010-10-15T09:27:37Z";
    OffsetDateTime odt = convert(dtStart);
    System.out.println(odt);
}

will print the line

2010-10-15T09:27:37Z

when there are the corresponding methods

public static OffsetDateTime convert(String datetime) {
    return OffsetDateTime.parse(datetime);
}

or

public static ZonedDateTime convert(String datetime) {
    return ZonedDateTime.parse(datetime);
}

but of course not in the same class, that would not compile...

There's a LocalDateTime, too, but that would not be able to parse a zone or offset.

If you want to use custom formats for parsing or formatting output, you can utilize a DateTimeFormatter, maybe like this:

public static void main(String[] args) {
    String dtStart = "2010-10-15T09:27:37Z";
    String converted = ZonedDateTime.parse(dtStart)
                                    .format(DateTimeFormatter.ofPattern(
                                                    "EEE MMM d HH:mm:ss zz uuuu",
                                                    Locale.ENGLISH
                                                )
                                            );
    System.out.println(converted);
}

which will output

Fri Oct 15 09:27:37 Z 2010

For an OffsetDateTime, you would need to adjust the pattern a little:

public static void main(String[] args) {
    String dtStart = "2010-10-15T09:27:37Z";
    String converted = OffsetDateTime.parse(dtStart)
                                    .format(DateTimeFormatter.ofPattern(
                                                    "EEE MMM d HH:mm:ss xxx uuuu",
                                                    Locale.ENGLISH
                                                )
                                            );
    System.out.println(converted);
}

This will produce a (slightly) different output:

Fri Oct 15 09:27:37 +00:00 2010

That's because a ZonedDateTime considers named time zones with changing offsets (due to daylight saving times or anything similar) while an OffsetDateTime just knows an offset from UTC.