how to change format of date from string date

2020-05-09 21:30发布

I have date as a string like this

String date = "11-12-2018"

I want to change it to "2018-12-11"

with the same variable. So, I tried the code below but it doesn't give me the output I expect.

String date = "11-12-2018"

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

Date d = df.parse(date);

results in: "0012-06-09"


I want "2018-12-11"

标签: java android
4条回答
甜甜的少女心
2楼-- · 2020-05-09 22:17

Try code below that will work for your case:

First parse your input format from string,

String date = "11-12-2018";
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");

Then convert it to desired format,

Date dateTobeParse = null;
try {
    dateTobeParse = df.parse(date);
} catch (ParseException e) {
    e.printStackTrace();
}
if (dateTobeParse != null) {
    SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
    String outputDate = outFormat.format(dateTobeParse);
}
查看更多
我只想做你的唯一
3楼-- · 2020-05-09 22:21

Try the code below that will work

1) Make method like below

public String changeDateFormat(String currentFormat, String requiredFormat, String dateString) {
        String 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;
    }//end of changeDateFormat()

1st argument of the method is your current date format in your case it will be 'dd-MM-yyyy'

2nd argument is output or requires date format in your case it will be 'yyyy-MM-dd'

3rd argument is your date that you want to change the format

2) Run the method like below

String oldFormatDate = "11-12-2018";
String myDate = changeDateFormat("dd-MM-yyyy", "yyyy-MM-dd", oldFormatDate);
Log.d(TAG, "Old formatted Date : " + oldFormatDate);
Log.d(TAG, "New Date is : " + myDate);

3) Output:

Old formatted Date : 11-12-2018
New Date is : 2018-12-11
查看更多
▲ chillily
4楼-- · 2020-05-09 22:22

You can do this 3 ways. First is using SimpleDateFormat and Date and second using DateTimeFormatter and LocalDate and third you can use Split.

1. Using Date and SimpleDateFormat

String date = "11-12-2018";
SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy");
java.util.Date d = df.parse(date);
String finalDate = new SimpleDateFormat("yyyy-MM-dd").format(d);
System.out.println(finalDate);

Here we have our actual date String date = "11-12-2018"; we know we want to change it to 2018-12-11

So lets parse that date into a Date object using this code

SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy");
java.util.Date d = df.parse(date);

Okay so now we have a date object of our actual date, Now lets format it to our new date.

String finalDate = new SimpleDateFormat("yyyy-MM-dd").format(d);

2. Using LocalDate and DateTimeFormatter

Alright here we define our date again and 2 DateTimeFormatter.

DateTimeFormatter oldFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

The first formatter is our old date format, and the second one is the new one that we are gonna convert the old date into.

Alright lets use them now!

Now we make a new LocalDate object using our oldFormatter by parsing our dateString with the oldFormatter object

LocalDate dateTime = LocalDate.parse(date, oldFormatter);

Alright now lets format it.

String reformattedDate = dateTime.format(newFormatter);

as simple as that! Here is the full code.

String date = "11-12-2018";
DateTimeFormatter oldFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate dateTime = LocalDate.parse(date, oldFormatter);
String reformattedDate = dateTime.format(newFormatter);
System.out.println(reformattedDate);

3. Using String::Split

Okay this part is pretty simple. Lets split the date using -

String[] dates = date.split("-");

We already know the order of the date lets format it using String::format

String reformattedDate = String.format("%s-%s-%s", dates[2], dates[1], dates[0]);

Here is the full code

String date = "11-12-2018";
String[] dates = date.split("-");
String reformattedDate = String.format("%s-%s-%s", dates[2], dates[1], dates[0]);
System.out.println(reformattedDate);
查看更多
小情绪 Triste *
5楼-- · 2020-05-09 22:36

This is the common function which I use for date and time conversion

public String convertDateAndTime(String date, String oldFormat, String newFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(oldFormat);
        Date currentdate;
        String converted = "";
        try {
            currentdate = sdf.parse(date);
            SimpleDateFormat sdf2 = new SimpleDateFormat(newFormat);
            converted = sdf2.format(currentdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return converted;
    }

You just have to pass the date string and their old and new formats. In your case call this function like this

String converteddate = convertDateAndTime("11-12-2018","dd-mm-yyyy","yyyy-MM-dd");
查看更多
登录 后发表回答