Total members 11895 |It is currently Tue Dec 03, 2024 5:47 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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;
    }
}
 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com