java util date - Time Zone throwing illegal argument exception

java util date - Time Zone throwing illegal argument exception

Sample Code

String timestamp1 = "Wed Mar 02 00:00:54 PST 2011";
Date d = new Date(timestamp1);
System.out.println(d.toString());

String timestamp2 = "Wed Mar 02 12:57:52 IST 2011";
Date d2 = new Date(timestamp2);
System.out.println(d2.toString());

The date object is not getting created for IST time zone. Java is throwing an Illegal Argument Exception Why is the second SOP always throwing an exception ? This piece of code is running on app servers on machines in different time zones ? How do I always make sure that the date gets created in a particular time zone ?

View Answers

March 2, 2011 at 5:29 PM

The toString() method already displaying IST by default.

Modified your code:

import java.util.*;
class Example 
{
    public static void main(String[] args) 
    {
        String timestamp1 = "Wed Mar 02 00:00:54 2011";
Date d = new Date(timestamp1);
System.out.println(d.toString());

String timestamp2 = "Wed Mar 02 12:57:52 2011";
Date d2 = new Date(timestamp2);
System.out.println(d2.toString());
    }
}









Related Tutorials/Questions & Answers:
Throwing an Exception
Throwing an Exception      ... statement to throw an exception.  The throw statement requires a single argument...;head> <title>Throwing an Exception in jsp</title>
Example to show Argument Exception in java
Example to show Argument Exception in java  ... from Argument exception in java .Exceptions are the condition  in Java... describes  how to handle Argument exceptions appropriately in your programs
Advertisements
Class Cast & illegal state Exception in blackberry
Class Cast & illegal state Exception in blackberry  tell me please i m facing class cast exception. when i click on Maps & iGoogle in Google Homepage on my emulator then it gives class cast & Illegal State exception
A Java program to simulate throwing a die
A Java program to simulate throwing a die  A Java program to simulate throwing a die (singular of dice) 6000 times and store the frequency with which each face of the die is uppermost after each throw in an array. A couple
Java argument passing
Java argument passing  How many ways can an argument be passed to a subroutine and explain them
illegal start of expression - Java Beginners
illegal start of expression  here i attach my program.. which shows...); getContentPane.add(label); getContentPane.add(text); } catch (Exception e... (Exception e){} } public static void main(String[] args){ Image im=new Image
Throwing an Exception in JSTL
Throwing an Exception in JSTL     ... this example you can better understand how the exception can be thrown in JSTL.  ..." prefix="c" %> <html> <head> <title>Throwing
Version of avalon-util>avalon-util-exception dependency
List of Version of avalon-util>avalon-util-exception dependency
Maven dependency for avalon-util - avalon-util-exception version 1.0.0 is released. Learn to use avalon-util-exception version 1.0.0 in Maven based Java projects
to use  avalon-util - avalon-util-exception version 1.0.0 in Java projects... and includes  avalon-util - avalon-util-exception version 1.0.0 java library...-exception released The developers of   avalon-util - avalon-util
Maven dependency for avalon-util - avalon-util-exception version 1.0 is released. Learn to use avalon-util-exception version 1.0 in Maven based Java projects
; avalon-util - avalon-util-exception version 1.0 in Java projects. Follow the step...; avalon-util - avalon-util-exception version 1.0 java library in your project...-exception released The developers of   avalon-util - avalon-util
Maven dependency for avalon-util - avalon-util-exception version 1.0-dev is released. Learn to use avalon-util-exception version 1.0-dev in Maven based Java projects
to use  avalon-util - avalon-util-exception version 1.0-dev in Java...-exception released The developers of   avalon-util - avalon-util..., the released version of  avalon-util - avalon-util-exception library
Exception Handlerin java..
Exception Handlerin java..  can a catch block accept multiple argument and return type?? please tell with explaination
What is command line argument in Java
What is command line argument in Java  What is command line argument in Java and how one can use it to create a program in Java? Can you post any example of command line argument program in Java?? It will be really helpful
Maven Repository/Dependency: avalon-util | avalon-util-exception
Maven Repository/Dependency of Group ID avalon-util and Artifact ID avalon-util-exception. Latest version of avalon-util:avalon-util-exception dependencies. # Version Release Date 1
Exception Java
Exception Java  Hi, What are the Exception Java? Thanks   Hi, Read it at Java Exception. Thanks
Java exception
Java exception  What happens if an exception is not caught
Java exception
Java exception  Can an exception be rethrown
Java Exception
Java Exception  Which is superclass of Exception
java exception
java exception   define an exception called no match exception tat z... { public static void main(String[] args) throws Exception { String z="Hello"; if(!z.equals("India")){ throw new Exception
Java exception
Java exception  What is the difference between Checked and Unchecked exception
Java exception
Java exception  What is the difference between exception and error
Java Generic program with command line argument
Java Generic program with command line argument  Implement Generic to perform arithmetic evaluation using command line argument by using Java
exception in java - Java Beginners
exception in java  exception in java
Java Throw Built-in Exception
Java Throw Built-in Exception In this section we are discussing about throwing of built-in exceptions in Java. In Java throw and throws keywords are used to throw an Exception. throw keyword throws an exception forcibly that may use
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for avalon-util-exception version 1.0.0
avalon-util:avalon-util-exception:1.0.0 Java library in your project.. Maven...-util-exception version 1.0.0. You be able to use the dependency given here for the  avalon-util-exception version 1.0.0 just by copying and pasting
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for avalon-util-exception version 1.0-dev
avalon-util:avalon-util-exception:1.0-dev Java library in your project.. Maven... for avalon-util-exception version 1.0-dev. You be able to use the dependency given here for the  avalon-util-exception version 1.0-dev just by copying
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for avalon-util-exception version 1.0
-util:avalon-util-exception:1.0 Java library in your project.. Maven, Gradle...; avalon-util-exception version 1.0 just by copying and pasting your project...: Group: avalon-util Artifact: avalon-util-exception ADS_TO_REPLACE_2 Version
ModuleNotFoundError: No module named 'Zone'
ModuleNotFoundError: No module named 'Zone'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'Zone' How to remove the ModuleNotFoundError: No module named 'Zone' error
Maven Dependency avalon-util-exception >> 1.0
You should include the dependency code given in this page to add Maven Dependency of avalon-util >> avalon-util-exception version1.0 in your project
Maven Dependency avalon-util-exception >> 1.0.0
You should include the dependency code given in this page to add Maven Dependency of avalon-util >> avalon-util-exception version1.0.0 in your project
exception in java
exception in java  StringIndexOutOfBounds Exception in flames program   Hi Friend, We have providing you a program. Try it:ADS_TO_REPLACE_1 import java.util.*; public class FlamesProgram{ static String name1
Exception - Java Beginners
. ------------------------------------------------ Read for more information http://www.roseindia.net/java/exceptions/exception... The Throwable class is the superclass of all errors and exceptions in the Java...) are thrown by the Java Virtual Machine or can be thrown by the Java throw
Java Exception
Java Exception  What are monitors
Java exception
Java exception  Can try statements be nested
Java Exception
Java Exception  What is the purpose of finalization
Java Exception
Java Exception  What is ?Resource leak
Java Exception
Java Exception  What are Chained Exceptions
Java exception
Java exception  What is NullPointerException and how to handle
Java Exception
Java Exception  Why Runtime Exceptions are Not Checked
Java Exception
Java Exception  Why Errors are Not Checked
Java Exception
Java Exception  Can a catch block exist without a try block
Java Exception
Java Exception  If I write System.exit (0); at the end of the try block, will the finally block still execute
Java exception handling
Java exception handling  what are the constraints imposed by overriding on exception handling
Java programme - Date Calendar
Java programme  Can you provide me the coding for Date class provided by java util package
null pointer exception in java
null pointer exception in java  What is null pointer exception in Java and when it occurs? Show it with an example.Thanks!   Java Null Pointer Exception
java exception handle
java exception handle  How error can be handled in the exception class?? can we handle fatal error in java
Java exception handling
Java exception handling  How does a try statement determine which catch clause should be used to handle an exception
Exception - Java Beginners
What is Exception in Java  What is the Exception that occurs while running the application in Java
Java exception handling
Java exception handling  How does a try statement determine which catch clause should be used to handle an exception
Java exception handling
Java exception handling  What is the catch or declare rule for method declarations

Ads