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

Ads









Related Tutorials/Questions & Answers:
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
Throwing an Exception
Throwing an Exception      ... statement to throw an exception.  The throw statement requires a single argument...; <title>Throwing an Exception in jsp</title> </head>
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
Version of avalon-util>avalon-util-exception dependency
List of Version of avalon-util>avalon-util-exception dependency
Throwing an Exception in JSTL
Throwing an Exception in JSTL     ... this example you can better understand how the exception can be thrown in JSTL.  ...;html> <head> <title>Throwing an Exception</title> <
Exception Java
Exception Java  Hi, What are the Exception Java? Thanks   Hi, Read it at Java Exception. Thanks
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
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
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... will demonstrate you about how to use throw keyword in Java to throw the exception
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
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
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
check null exception java
check null exception java  check null exception java - How to check the null exception on Java?   The null pointer exception in java occurs... on it. See the example null pointer exception
java question on exception
java question on exception  can we use inbuilt exception variable with throw keyword in java
Java Stream Write Exception
Java Stream Write Exception  I am doing socket programming. I have...(Exception e) { System.out.println("Exception :: "+e); } When the line out.writeUTF(data1) is executed and catch catches it and shows the exception
exception
exception  chek in and check out exception in java   Please visit the following link: Checked and Unchecked Exception
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
how to add date and change date in java - netbeans
how to add date and change date in java - netbeans  I need to code use to increase date How to get date after 30 days using netbeans
How to compare date in Java?
How to compare date in Java?  Hi, How to compare date in Java? Thanks   Hi, Check this example Date Comparison. Thanks
what is a null pointer exception in java
what is a null pointer exception in java  what is a null pointer exception in java
exception Handling
exception Handling  write a program in Java to display the names and roll numbrs of students. Initialize respective array variables for 10 students... illegal terminations of program
numberformat exception - Java Beginners
Exception. How can i handle
Exception
Exception  whis is the Arithmetic Exception in java? or define Arithmetic Exception with exp?   Arithmetic Exception occurs, when you divide a number by zero. Example public class TryCatch { public static void main
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger  Hi, How to solve following error? Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
illegal start of type
illegal start of type  Hi, This is my code i m getting illegal start...(); } catch(Exception e...("FileNotFoundException : " + ex); } catch(Exception ioe) { System.out.println
How to convert date to string in Java?
How to convert date to string in Java?  Hello developers, I have java.util.Date object and it has to be converted to date format. How to convert date to string in Java? Thanks   Hi, To convert java.util.Date object
How to create custom exception in Java?
How to create custom exception in Java? In this tutorial we... custom exception class. Custom exceptions are used by Java developers to create.... Lets start developing our custom exception class.In Java API all exception
exception
exception  wHEN RUNTIME EXCEPTION CLASS IS SUB OF EXCEPTION CLASS HOW CAN'T HANDLE UNCHECKED EXCEPTION

Ads