Package br.com.colegio.util

Source Code of br.com.colegio.util.UtilData

package br.com.colegio.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

public class UtilData
{
  public static Date parser(String str)
  {
    try
    {
      SimpleDateFormat sdf = new SimpleDateFormat();

      sdf.applyPattern("dd/MM/yyyy");

      return sdf.parse(str);
    }
    catch (ParseException e)
    {
//      e.printStackTrace();
    }

    return null;
  }

  public static Date parser(String str, String format)
  {
    try
    {
      SimpleDateFormat sdf = new SimpleDateFormat();

      sdf.applyPattern(format);

      return sdf.parse(str);
    }
    catch (ParseException e)
    {
      e.printStackTrace();
    }

    return null;
  }

  public static String format(Date dat, String format)
  {
    SimpleDateFormat sdf = new SimpleDateFormat();

    sdf.applyPattern(format);

    return sdf.format(dat);
  }

  public static String en2pt(String str)
  {
    str = str.replaceAll("Feb", "Fev");
    str = str.replaceAll("Apr", "Abr");
    str = str.replaceAll("May", "Mai");
    str = str.replaceAll("Aug", "Ago");
    str = str.replaceAll("Sep", "Set");
    str = str.replaceAll("Oct", "Out");
    str = str.replaceAll("Dec", "Dez");
   
    return str;
  }

  public static long diffDays(Date d1, Date d2)
  {
    return (d1.getTime() - d2.getTime()) / (24 * 60 * 60 * 1000);
  }

  public static void main(String[] args)
  {
    System.out.println(UtilData.parser(UtilData.en2pt("20-Dec-2006"), "dd-MMM-yyyy"));
    System.out.println(UtilData.parser(UtilData.en2pt("30/04/12"), "dd/MM/yy"));
    System.out.println(UtilData.format(new Date(), "yyyyMM"));
    System.out.println(UtilData.format((new GregorianCalendar(2013, 1, 1)).getTime(), "yyyyMM"));
    System.out.println(UtilData.parser("Tue Apr 10 00:00:00 AMT 2012"));
//    System.out.println(UtilData.diffDays(new Date(), new Date(2012-1900,6-1,1)));
    //System.out.println(UtilData.format(new Date(112,12,1,16,22,55), "yyyy-MM-dd.HHmmss"));
  }
}
TOP

Related Classes of br.com.colegio.util.UtilData

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.