Get Date stamp : Date Time Timestamp « Database SQL JDBC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Database SQL JDBC » Date Time TimestampScreenshots 
Get Date stamp
     
/*
 * DateTimeUtil.java
 *
 * Copyright (C) 2005-2009 Tommi Laukkanen
 * http://www.substanceofcode.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

//package com.substanceofcode.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * <p>This class provides methods for dealing with Dates and Times.</p>
 
 * In particular it deals with converting Dates/Times to String representations of those values.
 
 @author Tommi Laukkanen
 @author Barry Redmond
 */
public abstract class DateTimeUtil {

    /** Get current Date stamp
     *  @return The Curretn Date/Time in the format: yyyymmdd_hhmm 
     */
    public static String getCurrentDateStamp() {
        return convertToDateStamp(System.currentTimeMillis());
    }
    
    /** Convert given date to string<br>
     *  OutputFormat: yyyymmdd_hhmm
     *  @return The Date/Time in the format: yyyymmdd_hhmm
     */
    public static String convertToDateStamp(long time){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(time));
        return convertToDateStamp(cal);
    }
    
    /** Convert given date to string<br>
     *  OutputFormat: yyyymmdd_hhmm
     *  @return The Date/Time in the format: yyyymmdd_hhmm
     */
    public static String convertToDateStamp(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return convertToDateStamp(cal);
    }
    
    /** Convert given date to string<br>
     *  OutputFormat: yyyymmdd_hhmm
     *  @return The Date/Time in the format: yyyymmdd_hhmm
     */
    public static String convertToDateStamp(Calendar cal) {
        String year = String.valueOf(cal.get(Calendar.YEAR));
        String month = String.valueOf(cal.get(Calendar.MONTH1);
        if (month.length() == 1) {
            month = "0" + month;
        }
        String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
        if (day.length() == 1) {
            day = "0" + day;
        }
        String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
        if (hour.length() == 1) {
            hour = "0" + hour;
        }
        String minute = String.valueOf(cal.get(Calendar.MINUTE));
        if (minute.length() == 1) {
            minute = "0" + minute;
        }
        String second = String.valueOf(cal.get(Calendar.SECOND));
        if (second.length() == 1) {
            second = "0" + second;
        }        
        String dateStamp = year + month + day + "_" + hour + minute + second;
        return dateStamp;
    }
    
    /** 
     * Get current time stamp in universal format<br>
     * Format: yyyy-mm-ddThh:mm:ssZ<br>
     * e.g.: 1999-09-09T13:10:40Z
     @return The Date in the format: yyyy-mm-ddThh:mm:ssZ
     */
    public static String getUniversalDateStamp(long time){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(time));
        return getUniversalDateStamp(cal);
    }

    /**
     * Get date from given date stamp.
     * Format: yyyy-mm-ddThh:mm:ssZ
     @param stamp
     @return
     */
    public static Date getDateFromUniversalDateStamp(String stamp) {
 //       Log.debug("Parsing date: " + stamp);
        String year = stamp.substring(0,4);
        String month = stamp.substring(5,7);
        String day = stamp.substring(8,10);
        String hour = stamp.substring11,13 );
        String minutes = stamp.substring14,16 );
        String seconds = stamp.substring17,19 );

        //Log.debug(year+"-"+month+"-"+day+"T"+hour+":"+minutes+":"+seconds);

        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("UNC+0"));
        cal.set(Calendar.YEAR, Integer.parseInt(year) );
        cal.set(Calendar.MONTH, Integer.parseInt(month)-);
        cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day) );
        cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour) );
        cal.set(Calendar.MINUTE, Integer.parseInt(minutes) );
        cal.set(Calendar.SECOND, Integer.parseInt(seconds) );

        Date date = cal.getTime();
        return date;
    }
    
    /** 
     * Get current time stamp in universal format<br>
     * Format: yyyy-mm-ddThh:mm:ssZ<br>
     * e.g.: 1999-09-09T13:10:40Z
     @return The Date in the format: yyyy-mm-ddThh:mm:ssZ
     */
    public static String getUniversalDateStamp(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return getUniversalDateStamp(cal);
    }
    
    /** 
     * Get current time stamp in universal format<br>
     * Format: yyyy-mm-ddThh:mm:ssZ<br>
     * e.g.: 1999-09-09T13:10:40Z
     @return The Date in the format: yyyy-mm-ddThh:mm:ssZ  
     */
    public static String getUniversalDateStamp(Calendar cal) {
        String year = String.valueOf(cal.get(Calendar.YEAR));
        String month = String.valueOf(cal.get(Calendar.MONTH1);
        if (month.length() == 1) {
            month = "0" + month;
        }
        String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
        if (day.length() == 1) {
            day = "0" + day;
        }
        String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
        if (hour.length() == 1) {
            hour = "0" + hour;
        }
        String minute = String.valueOf(cal.get(Calendar.MINUTE));
        if (minute.length() == 1) {
            minute = "0" + minute;
        }
        String second = String.valueOf(cal.get(Calendar.SECOND));
        if (second.length() == 1) {
            second = "0" + second;
        }
        String dateStamp = year + "-" + month + "-" + day + "T" + hour + ":"
                + minute + ":" + second + "Z";
        return dateStamp;
    }
    
    /** Convert date to short time string 
     @return The Date in the format: hh:mm:ss  
     */
    public static String convertToTimeStamp(long time){
        return convertToTimeStamp(time, true);
    }
    
    /** Convert date to short time string 
     @return The Date in the format: hh:mm:ss  
     */
    public static String convertToTimeStamp(Date date) {
        return convertToTimeStamp(date, true);
    }
    /** Convert date to short time string 
     @return The Date in the format: hh:mm:ss  
     */
    public static String convertToTimeStpam(Calendar cal){
        return convertToTimeStamp(cal, true);
    }
    
    /** Convert date to short time string 
     @param showSeconds Wheather or not to show just the hours and minutes part, or to show the seconds part also.
     @return The Date in the format: hh:mm:ss  
     */
    public static String convertToTimeStamp(long time, boolean showSeconds) {
        return convertToTimeStamp(new Date(time), showSeconds);
    }

    /** Convert date to short time string 
     @param showSeconds Wheather or not to show just the hours and minutes part, or to show the seconds part also.
     @return The Date in the format: hh:mm:ss  
     */
    public static String convertToTimeStamp(Date date, boolean showSeconds) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return convertToTimeStamp(c, showSeconds);
    }

    /**
     
     @param time 
     @param showSeconds Wheather or not to show just the hours and minutes part, or to show the seconds part also.
     @return The Date in the format: hh:mm:ss  */
    public static String convertToTimeStamp(Calendar time, boolean showSeconds) {
        String hours = Integer.toString(time.get(Calendar.HOUR_OF_DAY));
        if (hours.length() == 1) {
            hours = '0' + hours;
        }
        String minutes = Integer.toString(time.get(Calendar.MINUTE));
        if (minutes.length() == 1) {
            minutes = '0' + minutes;
        }
        if (showSeconds) {
            String seconds = Integer.toString(time.get(Calendar.SECOND));
            if (seconds.length() == 1) {
                seconds = '0' + seconds;
            }
            return hours + ":" + minutes + ":" + seconds;
        else {
            return hours + ":" + minutes;
        }
    }
    
    /** 
     
     @param startDate Interval start date time
     @param endDate Interval end date time
     @return Time interval in format hh:mm:ss
     */
    public static String getTimeInterval(Date startDate, Date endDate) {
        long interval = (endDate.getTime() - startDate.getTime());
        if(interval == 0) { return "00:00:00"}
        long intervalSeconds = interval / 1000;
        long hours = intervalSeconds / 3600;
        long minutes = (intervalSeconds % 360060;
        long seconds = intervalSeconds % 60;
        String hoursText = String.valueOfhours );
        if(hoursText.length()==1) { hoursText = "0" + hoursText; }
        String minutesText = String.valueOfminutes );
        if(minutesText.length()==1) { minutesText = "0" + minutesText; 
        String secondsText = String.valueOfseconds );
        if(secondsText.length()==1) { secondsText = "0" + secondsText; }
        return hoursText + ":" + minutesText + ":" + secondsText;
    }
}

   
    
    
    
    
  
Related examples in the same category
1.Convert java.sql.Timestamp to long for easy compare
2.Get Date From MySql
3.Get java.sql.Timestamp fro current time
4.Get date from Oracle
5.Insert Date, time and date time data to Oracle
6.Construct java.sql.Timestamp from string
7.Demo PreparedStatement Set Time
8.Demo PreparedStatement Set Timestamp
9.Demo PreparedStatement Set Date
10.Compare two times
11.Convert an Object to a DateTime, without an Exception
12.Convert an Object to a Timestamp, without an Exception
13.Convert an Object to a java.sql.Time
14.Timestamp parse
15.Parse date and time
16.convert Strings to Dates and Timestamps and vice versa.
17.Convert into java.sql.Time (or into java.util.Calendar)
18.A method to get the current date and time to use in a timestamp
19.A method to get the current date to use in a timestamp
20.Get today's Timestamp
21.Convert String To Timestamp
22.Format Timestamp
23.Convert a timestamp (= millisecs) to a concise string
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.