yshak
yshak

Reputation: 2191

Conversion of Date

I am getting date in two different formats.
1) 2012-01-05
2) 05/01/2012
But I want this to be in the below format. "5 Jan 2011"

Now I'm having String d1="2012-01-05" and String d2="2012-01-05".

Upvotes: 0

Views: 999

Answers (5)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 78945

java.time

You need a DateTimeFormatter with two optional patterns for parsing your date-time strings into instances of LocalDate. Then, you need a DateTimeFormatter to format the LocalDate instances into the desired pattern.

Demo:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter parser = DateTimeFormatter.ofPattern("[uuuu-MM-dd][dd/MM/uuuu]", Locale.ENGLISH);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d MMM uuuu", Locale.ENGLISH);

        Stream.of(
                "2012-01-05",
                "05/01/2012"
            )
            .map(s -> LocalDate.parse(s, parser))
            .map(d -> d.format(formatter))
            .forEach(System.out::println);
    }
}

Output:

5 Jan 2012
5 Jan 2012

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Upvotes: 1

litterbugkid
litterbugkid

Reputation: 3666

This should retrieve the date in the format you want. Simple few lines.

            String pattern = "dd MMM yyyy";
            SimpleDateFormat format = new SimpleDateFormat(pattern);

            System.out.println(format.format(new Date()));

Upvotes: 0

Harsh Trivedi
Harsh Trivedi

Reputation: 1010

String d1 = "2012-01-05";
String d2 = "05/01/2012";
SimpleDateFormat curFormater1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat curFormater2 = new SimpleDateFormat("dd/MM/yyyy");
Date dateObj1 = null;

Date dateObj2 = null;
try {
    dateObj1 = curFormater.parse(d1);
    dateObj2 = curFormater.parse(d2);
} catch (ParseException e) {
    e.printStackTrace();
}

SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
String date_new1 = formatter.format(d1);
String date_new2 = formatter.format(d2);

try this code it will help you so solve your problem

Upvotes: 0

Christian Kuetbach
Christian Kuetbach

Reputation: 16060

Take a look here: How to parse a date?

You have to determine, which format is used and than create a appropriate pattern.

After parsing the date, you can format it with another pattern, if you need a String:

SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");  
String now = formatter.format(new Date());

Upvotes: 0

NikhilReddy
NikhilReddy

Reputation: 6954

SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");  
String now = formatter.format(new Date());

this u want right...

or

String oldString = "2009-12 Dec";
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("yyyy-MM").parse(oldString)); // Yes, month name is ignored but we don't need this.
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
String newString = new SimpleDateFormat("dd-MMM-yyyy").format(calendar.getTime()).toUpperCase();
System.out.println(newString); // 31-DEC-2009

Upvotes: 1

Related Questions