Convert date from mmm dd, yyyy (May 12, 2011) to yyyy-mm-dd - Help Appreciated
I have developed the following script, but I can not get the if statements to work in the month and the date section. Any suggestions would be helpful. Thanks.
//reformat date from mmm dd, yyyy to yyyy-mm-dd
try{
// Local reference to variables
String rvwDate = dataRecord.get("Date");
if(rvwDate != null){
// Split the Original Date on the " " character into three parts
String[] dateParts = rvwDate.split(" ");
session.log( "Month is : " + dateParts[0] );
String mmValue;
//Translate the month into MM format
String mmPart = dateParts[0];
if(mmPart == "Jan.")
{
mmValue = "01";
}
if(mmPart == "Feb.")
{
mmValue = "02";
}
if(mmPart == "March")
{
mmValue = "03";
}
if(mmPart == "Apr.")
{
mmValue = "04";
}
if(mmPart == "May")
{
mmValue = "05";
}
if(mmPart == "June")
{
mmValue = "06";
}
if(mmPart == "July")
{
mmValue = "07";
}
if(mmPart == "Aug.")
{
mmValue = "08";
}
if(mmPart == "Sept.")
{
mmValue = "09";
}
if(mmPart == "Oct.")
{
mmValue = "10";
}
if(mmPart == "Nov.")
{
mmValue = "11";
}
if(mmPart == "Dec.")
{
mmValue = "12";
}
session.log ("Day is : " + dateParts[1]);
//add 0 prefix to 1-9 DD values
String ddValue;
String ddValueLong = dateParts[1];
ddValue = ddValueLong.substring(0,2);
session.log ("two postion dateParts: " + ddValue);
if(ddValue == "1,")
ddValue = "01";
if(ddValue == "2,")
ddValue = "02";
if(ddValue == "3,")
ddValue = "03";
if(ddValue == "4,")
ddValue = "04";
if(ddValue == "5,")
ddValue = "05";
if(ddValue == "6,")
ddValue = "06";
if(ddValue == "7,")
ddValue = "07";
if(ddValue == "8,")
ddValue = "08";
if(ddValue == "9,")
ddValue = "09";
session.log ("Year is : " + dateParts[2]);
//Assemble the correct date format yyyy-mm-dd
rvwDate = dateParts[2] + "-" + mmValue + "-" + ddValue;
// Put new date in dataRecord
dataRecord.put("Date", rvwDate);
}
}
catch(Exception e){
session.log("Error running Fix Zip Codes and Nulls");
}
try{
// Local reference to variables
String rvwDate = dataRecord.get("Date");
if(rvwDate != null){
// Split the Original Date on the " " character into three parts
String[] dateParts = rvwDate.split(" ");
session.log( "Month is : " + dateParts[0] );
String mmValue;
//Translate the month into MM format
String mmPart = dateParts[0];
if(mmPart == "Jan.")
{
mmValue = "01";
}
if(mmPart == "Feb.")
{
mmValue = "02";
}
if(mmPart == "March")
{
mmValue = "03";
}
if(mmPart == "Apr.")
{
mmValue = "04";
}
if(mmPart == "May")
{
mmValue = "05";
}
if(mmPart == "June")
{
mmValue = "06";
}
if(mmPart == "July")
{
mmValue = "07";
}
if(mmPart == "Aug.")
{
mmValue = "08";
}
if(mmPart == "Sept.")
{
mmValue = "09";
}
if(mmPart == "Oct.")
{
mmValue = "10";
}
if(mmPart == "Nov.")
{
mmValue = "11";
}
if(mmPart == "Dec.")
{
mmValue = "12";
}
session.log ("Day is : " + dateParts[1]);
//add 0 prefix to 1-9 DD values
String ddValue;
String ddValueLong = dateParts[1];
ddValue = ddValueLong.substring(0,2);
session.log ("two postion dateParts: " + ddValue);
if(ddValue == "1,")
ddValue = "01";
if(ddValue == "2,")
ddValue = "02";
if(ddValue == "3,")
ddValue = "03";
if(ddValue == "4,")
ddValue = "04";
if(ddValue == "5,")
ddValue = "05";
if(ddValue == "6,")
ddValue = "06";
if(ddValue == "7,")
ddValue = "07";
if(ddValue == "8,")
ddValue = "08";
if(ddValue == "9,")
ddValue = "09";
session.log ("Year is : " + dateParts[2]);
//Assemble the correct date format yyyy-mm-dd
rvwDate = dateParts[2] + "-" + mmValue + "-" + ddValue;
// Put new date in dataRecord
dataRecord.put("Date", rvwDate);
}
}
catch(Exception e){
session.log("Error running Fix Zip Codes and Nulls");
}
Actually you just need
Actually you just need sutil.reformatDate()
It would look like this:
newDate = sutil.refomatDate(rvwDate, "MMM dd, yyyy", "yyyy-MM-dd");
dataRecord.put("Date", newDate);
Did not work as planned
Jason - I have 2 issues that didn't allow the above. The first issue is the many of the months (etc, Feb., Dec.) are abbreviated (period in the fourth position). The second issue is that some of the months are spelled out and great than three characters (etc, July, Jun). Any slight changes to this reformatDate to help resolve this? Thanks.
rvwDate =
if (rvwDate!=null)
{
// Strip dots
rvwDate = rvwDate.replaceAll("\\.", "");
// Light dateFormat should catch any format of month day, year
newDate = sutil.refomatDate(rvwDate, "M d, yyyy", "yyyy-MM-dd");
dataRecord.put("Date", newDate);
}