Switch to full style
General Java code examples
Post a reply

date formatting in Java

Sat Nov 27, 2010 9:49 pm

The Following code show you how to formate the date using the date object and the pattern string (can be :ddmmyyyy,dd/mm/yy ,, etc ) . You can convert from string to date and from date to string .

Code:


import java
.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;


public class DateUtils {

    public static String formateDate(Date date, String pattern) {

        SimpleDateFormat formatter = new SimpleDateFormat(pattern);

        return formatter.format(date);

    }

    public Date convertToDate(String strDate, String pattern) {

        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        Date date = null;
        System.out.println("Try to convert= " + strDate + " using= " + pattern);
        try {
            date = (Date) formatter.parse(strDate);
        } catch (ParseException ex) {
            Logger.getLogger(DateUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return date;
    }
}
 




Post a reply
  Related Posts  to : date formatting in Java
 Formatting a Date with date()     -  
 Want start and end date of a month from current date     -  
 Date in Java     -  
 formatting string in c++     -  
 Formatting dates in mysql     -  
 get next date by next day     -  
 String to sql.Date     -  
 How To Increment Date In JSP?     -  
 date validation in asp     -  
 Google indexing DATE     -  

Topic Tags

Java Time