Change date format in a Java string Change date format in a Java string java java

Change date format in a Java string


Use LocalDateTime#parse() (or ZonedDateTime#parse() if the string happens to contain a time zone part) to parse a String in a certain pattern into a LocalDateTime.

String oldstring = "2011-01-18 00:00:00.0";LocalDateTime datetime = LocalDateTime.parse(oldstring, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"));

Use LocalDateTime#format() (or ZonedDateTime#format()) to format a LocalDateTime into a String in a certain pattern.

String newstring = datetime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));System.out.println(newstring); // 2011-01-18

Or, when you're not on Java 8 yet, use SimpleDateFormat#parse() to parse a String in a certain pattern into a Date.

String oldstring = "2011-01-18 00:00:00.0";Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(oldstring);

Use SimpleDateFormat#format() to format a Date into a String in a certain pattern.

String newstring = new SimpleDateFormat("yyyy-MM-dd").format(date);System.out.println(newstring); // 2011-01-18

See also:


Update: as per your failed attempt: the patterns are case sensitive. Read the java.text.SimpleDateFormat javadoc what the individual parts stands for. So stands for example M for months and m for minutes. Also, years exist of four digits yyyy, not five yyyyy. Look closer at the code snippets I posted here above.


Formatting are CASE-SENSITIVE so USE MM for month not mm (this is for minute) and yyyy For Reference you can use following cheatsheet.

G   Era designator  Text    ADy   Year    Year    1996; 96Y   Week year   Year    2009; 09M   Month in year   Month   July; Jul; 07w   Week in year    Number  27W   Week in month   Number  2D   Day in year Number  189d   Day in month    Number  10F   Day of week in month    Number  2E   Day name in week    Text    Tuesday; Tueu   Day number of week (1 = Monday, ..., 7 = Sunday)    Number  1a   Am/pm marker    Text    PMH   Hour in day (0-23)  Number  0k   Hour in day (1-24)  Number  24K   Hour in am/pm (0-11)    Number  0h   Hour in am/pm (1-12)    Number  12m   Minute in hour  Number  30s   Second in minute    Number  55S   Millisecond Number  978z   Time zone   General time zone   Pacific Standard Time; PST; GMT-08:00Z   Time zone   RFC 822 time zone   -0800X   Time zone   ISO 8601 time zone  -08; -0800; -08:00

Examples:

"yyyy.MM.dd G 'at' HH:mm:ss z"  2001.07.04 AD at 12:08:56 PDT"EEE, MMM d, ''yy"  Wed, Jul 4, '01"h:mm a"    12:08 PM"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time"K:mm a, z" 0:08 PM, PDT"yyyyy.MMMMM.dd GGG hh:mm aaa"  02001.July.04 AD 12:08 PM"EEE, d MMM yyyy HH:mm:ss Z"    Wed, 4 Jul 2001 12:08:56 -0700"yyMMddHHmmssZ" 010704120856-0700"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"   2001-07-04T12:08:56.235-0700"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"   2001-07-04T12:08:56.235-07:00"YYYY-'W'ww-u"  2001-W27-3


The answer is of course to create a SimpleDateFormat object and use it to parse Strings to Date and to format Dates to Strings. If you've tried SimpleDateFormat and it didn't work, then please show your code and any errors you may receive.

Addendum: "mm" in the format String is not the same as "MM". Use MM for months and mm for minutes. Also, yyyyy is not the same as yyyy. e.g.,:

import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class FormateDate {    public static void main(String[] args) throws ParseException {        String date_s = "2011-01-18 00:00:00.0";        // *** note that it's "yyyy-MM-dd hh:mm:ss" not "yyyy-mm-dd hh:mm:ss"          SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");        Date date = dt.parse(date_s);        // *** same for the format String below        SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd");        System.out.println(dt1.format(date));    }}