Convert a timestamp (= millisecs) to a concise string : 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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Database SQL JDBC » Date Time Timestamp 




Convert a timestamp (= millisecs) to a concise string
   
/**
 * Copyright (c) 2006 Richard Rodgers
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//package com.monad.homerun.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * TimeUtil is a utility class with static methods to convert times in various
 * formats into other formats
 */

public class TimeUtil {
  private static final int MINS_PER_DAY = 60 24;
  private static final long MS_PER_DAY = 1000 60 * MINS_PER_DAY;

  private static final int SEC = 1000;
  private static final int MIN = SEC * 60;
  private static final int HOUR = MIN * 60;
  private static final int DAY = HOUR * 24;
  private static final long WEEK = DAY * 7;
  private static final long YEAR = WEEK * 52;

  private static final long[] buckets = YEAR, WEEK, DAY, HOUR, MIN, SEC };
  private static final String[] bucketNames = "year""week""day",
      "hour""minute""second" };

  private static GregorianCalendar statFmtCal = new GregorianCalendar();

  private static final String ts24Pat = "H:mm:ss yy-MM-dd";


  /**
   * Convert a timestamp (= millisecs) to a concise string
   
   @param timestamp
   *            a date/time in milliseconds
   @return formatted time
   */
  public static String timeStampFormat(long timestamp) {
    // test for uninitialized time
    if (timestamp == -1L) {
      return "??";
    }
    SimpleDateFormat sdf = new SimpleDateFormat(ts24Pat);
    return sdf.format(new Date(timestamp));
  }



}

   
    
    
  














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.Get Date stamp
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.