The Artima Developer Community
Sponsored Link

Java Answers Forum
Translate a timestamp to seconds?

2 replies on 1 page. Most recent reply: Apr 4, 2003 12:38 AM by Kristin

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Kristin

Posts: 6
Nickname: kristin
Registered: Feb, 2003

Translate a timestamp to seconds? Posted: Apr 3, 2003 12:44 AM
Reply to this message Reply
Advertisement
I want to translate a timestamp in the form: 2003-04-03 09:10:40.0 to someting I can calculate with, like seconds or someting. Is this possible?


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Translate a timestamp to seconds? Posted: Apr 3, 2003 5:59 AM
Reply to this message Reply
import java.util.*;
import java.text.* ;
 
public class Test {
	public static void main(String[] args){
		String input = "2003-04-03 09:10:40.0";
		String pattern = "yyyy-MM-dd HH:mm:ss.S" ;
		SimpleDateFormat sdf = new SimpleDateFormat ( pattern );
		// Parse the text into a Date object
		Date dt = sdf.parse ( input, new ParsePosition(0) );
	
		// Get the Calendar instance to get components of date time
		Calendar cal = Calendar.getInstance ( ) ;
			
		// Set the time
		cal.setTime ( dt ) ;
 
		// Print time parts
		System.out.println ( "Hour:" + cal.get ( Calendar.HOUR ) ) ;
		System.out.println ( "Minute:" + cal.get ( Calendar.MINUTE ) ) ;
		System.out.println ( "Second:" + cal.get ( Calendar.SECOND ) ) ;
		System.out.println ( "Millisecond:" + cal.get ( Calendar.MILLISECOND ) ) ;
 
	}
	
}

Kristin

Posts: 6
Nickname: kristin
Registered: Feb, 2003

Re: Translate a timestamp to seconds? Posted: Apr 4, 2003 12:38 AM
Reply to this message Reply
Many thanks for the help!! =)

Flat View: This topic has 2 replies on 1 page
Topic: ArrayList Performance Previous Topic   Next Topic Topic: Reading files in a folder

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use