0

I need to change the date format from 2015-04-08 to 08-APR-2015. It is coming from grails gsp front end. Before calling oracle package I need to change the format in java. How to do it.

Downgoat
  • 13,771
  • 5
  • 46
  • 69
  • 2
    Do you want to format date at client side (javascript) or server side (java)? because you mentioned java in your question while tagged javascript? – Sameer Azazi Apr 27 '15 at 05:02

3 Answers3

2

I use SimpleDateFormat in changing date formats. Try this:

String OLD_FORMAT = "yyyy-MM-dd";
String NEW_FORMAT = "dd-MMM-yyyy";

String oldDateString = "2015-04-08";
String newDateString;

SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
Date d = sdf.parse(oldDateString);
sdf.applyPattern(NEW_FORMAT);
newDateString = sdf.format(d);
ThEpRoGrAmMiNgNoOb
  • 1,256
  • 3
  • 23
  • 46
1

You mentioned Java in your answer but you tagged JavaScript I've edited your question to show the Java tag. This is a JavaScript soution.

Assuming str is you input:

str.split('-').forEach(function(a,i,b){
    result += i===0?(b[2]+'-'):i===1?(((new Date(str)).toUTCString()).split(' ')[2]+'-').toUpperCase():a[0];
});
Downgoat
  • 13,771
  • 5
  • 46
  • 69
1

Ok I already see and answer but still want to add one.I am not sure you want JS or Java solution. You can this using Javascript or Java. Below are both ways:
Javascript

var c = new Date('2015-04-08');
locale = "en-us" 
function formatDate(d)
 {
    var month = d.toLocaleString(locale, { month: "long" });
    var day = d.getDate();
    day = day + "";
    if (day.length == 1)
     {
     day = "0" + day;
      }
      return day + '-' + month +'-' + d.getFullYear();
  }

And if you want same in Java you do below..
Java

String myDateString = "2015-04-08";
String reqDateString = new SimpleDateFormat("dd-MMM-yyyy").format(myDateString);

Better if done in Java looks more simpler and reliable..

Viraj Nalawade
  • 3,137
  • 3
  • 28
  • 44