Date parsing perversion /2

Thanks Michele for sending in!

             public String formatDate (String value, String format)
             {
                       
                        String regexTrim = "[;:-]|[\\.]|[\\s]";
                        String[] splitted = value.split(regexTrim);
                       
                        int anno = 0;
                        int mese = 0;
                        int giorno = 0;
                        int ora = 0;
                        int minuti = 0;
                        int secondi = 0;
                        int millisecondi = 0;
                       
                       
                        for(int i=0; i<splitted.length; i++)
                        {
                                   switch (i) {
            case 0:  if(!splitted[0].trim().equals("")){anno = Integer.parseInt(splitted[0]);}; break;
            case 1:  if(!splitted[1].trim().equals("")){mese = Integer.parseInt(splitted[1]);}; break;
            case 2:  if(!splitted[2].trim().equals("")){giorno = Integer.parseInt(splitted[2]);}; break;
            case 3:  if(!splitted[3].trim().equals("")){ora = Integer.parseInt(splitted[3]);}; break;
            case 4:  if(!splitted[4].trim().equals("")){minuti = Integer.parseInt(splitted[4]);}; break;
            case 5:  if(!splitted[5].trim().equals("")){secondi = Integer.parseInt(splitted[5]);}; break;
            case 6:  if(!splitted[6].trim().equals("")){millisecondi = Integer.parseInt(splitted[6]);}; break;
            default: logger.error("FORMAT PLACEHOLDERS (FAC) - Error"); return value;
                                   }
                        }
                       
                        Calendar calendar = new GregorianCalendar(anno,mese,giorno,ora,minuti,secondi);
                        calendar.add(Calendar.MILLISECOND, millisecondi);
                       
                        Date date = calendar.getTime();
                       
                        SimpleDateFormat sdf = new SimpleDateFormat(format);
                       
                        value = sdf.format(date);
                       
                        return value;
                       
            }

At last, it works.

Michele found it in the codebase of our beloved major italian bank

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s