Change Date Format in a Java String

Formatting a String date into a different format in Java

Use this code to format your `2017-05-23T06:25:50'

String strDate = "2017-05-23T06:25:50";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(strDate);
SimpleDateFormat sdfnewformat = new SimpleDateFormat("MMM dd yyyy");
String finalDateString = sdfnewformat.format(convertedDate);
} catch (ParseException e) {
e.printStackTrace();
}

The converted finalDateString set to your textView

Change date format dd-MM-yyyy to yyyy-MM-dd in Java

Try this (see update below)

try {
String startDateString = "08-12-2017";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf2.format(sdf.parse(startDateString)));
} catch (ParseException e) {
e.printStackTrace();
}

Update - Java 8

    String startDateString = "08-12-2017";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(LocalDate.parse(startDateString, formatter).format(formatter2));

How can I change the date format in Java?

How to convert from one date format to another using SimpleDateFormat:

final String OLD_FORMAT = "dd/MM/yyyy";
final String NEW_FORMAT = "yyyy/MM/dd";

// August 12, 2010
String oldDateString = "12/08/2010";
String newDateString;

SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
Date d = sdf.parse(oldDateString);
sdf.applyPattern(NEW_FORMAT);
newDateString = sdf.format(d);

Convert String Date to String date different format

SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
Date date = format1.parse("2013-02-21");
System.out.println(format2.format(date));

String to date format and vice versa

java.time

I strongly recommend that you use java.time, the modern Java date and time API, for your date work.

It generally takes two formatters for converting a string date in one format to a string in another format: one for describing the format you got, and one for the required format. In this case the former is built in. For your required result define a formatter statically:

private static final DateTimeFormatter DATE_FORMATTER
= DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(Locale.forLanguageTag("es-PA"));

The do:

    String dateFormat = "20211109";
LocalDate start = LocalDate.parse(dateFormat, DateTimeFormatter.BASIC_ISO_DATE);
String string = start.format(DATE_FORMATTER);
System.out.println(string);

Output:

11/09/2021

For formatting I took the built-in medium date format for Panama since this is one of the locales where the format fits what you asked for. You should of course use your users’ locale, not the one of Panama, for an output format that they will recognize as their own. In this way we are saving ourselves the trouble of composing a format pattern string, and at the same time the code lends itself excellently to internationalization.

You shouldn’t want to convert from one string format to another

If you are asking how to convert a date from one string format to another, you are really asking the wrong question. In all but the smallest throw-away programs, we should not handle dates as strings, but always store them in LocalDate objects. When we take string input, we parse. Only when we need to give string output, we format back.

Using a format pattern

If for one reason or another your users are not happy with Java’s localized format, and you need more control over the output format, you may use a format pattern as you tried in your question:

private static final DateTimeFormatter DATE_FORMATTER
= DateTimeFormatter.ofPattern("MM/dd/yyyy", Locale.ROOT);

Links

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Wikipedia article: ISO 8601
  • Related question: error output converting date format from YYYYMMDD to dd-mm-yyyy
  • Related question: String to date format java

How do I change the display of date of birth in java?

Hopefully, the date is retrieved as a java.sql.Date, which does not have a specific form.

To format such a date value, use:

java.sql.Date birthdate = resultSet.getDate("Birthdate");

SimpleDateFormat dateFmt = new SimpleDateFormat("dd/MM/yyyy");
String text = dateFmt.format(birthdate);

Or, in java.time (Java 8 and later):

java.time.LocalDate birthdate = resultSet.getDate("Birthdate").toLocalDate();

DateTimeFormatter dateFmt = DateTimeFormatter.ofPattern("dd/MM/uuuu");
String text = birthdate.format(dateFmt);

Convert java.util.Date to String

Convert a Date to a String using DateFormat#format method:

String pattern = "MM/dd/yyyy HH:mm:ss";

// Create an instance of SimpleDateFormat used for formatting
// the string representation of date according to the chosen pattern
DateFormat df = new SimpleDateFormat(pattern);

// Get the today date using Calendar object.
Date today = Calendar.getInstance().getTime();
// Using DateFormat format method we can create a string
// representation of a date with the defined format.
String todayAsString = df.format(today);

// Print the result!
System.out.println("Today is: " + todayAsString);

From http://www.kodejava.org/examples/86.html



Related Topics



Leave a reply



Submit