开发者

Date exception when trying to use date method

开发者 https://www.devze.com 2023-02-02 20:30 出处:网络
I have defined a object model where one of the array elements is a string public static String[] columnNames6

I have defined a object model where one of the array elements is a string

public static String[] columnNames6
          = {"Total Shares",
             "Total Calls",
             "Call Strike",
             "Call Premium",
             "Call Expiry"
            };

public static Object[][] data6
= {
         { new Double(0), new Double(0), new Double(0), new Double(0),"dd/mm/yyyy"},
       };

I then use the following code to get the date so that I can use the data method but having no joy - Can someone please tell me why it is throwing an exception after I do this

String ExpiryDate = (String)GV.data6[0][4];
System.out.println("DATE STRING IS:   " + ExpiryDate);

Date EndOptionDate = new Date(ExpiryDate); // SOMETHING WRONG HERE even thoug开发者_C百科h it compiles okay

//Get Todays's Date
  Date TodaysDate = new Date();

  //Calculate Days Option Expiry
 long DaysDifference = EndOptionDate.getTime() - TodaysDate.getTime();

Would really appreciate some help as really stuck not sure how I should code the line in bold - new to java, so please excuses my lack of knowledge looked at tutorials can't seem to move forward.

Thanks

Simon


ExpiryDate is a string try changing it to a date, its deprecated

Date(String s) Deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).

Here's an example:

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

Date today = df.parse("25/12/2010");            

System.out.println("Today = " + df.format(today));


As said by JonH, you should use DateFormat.parse instead of new Date(String). Beside, your calculation of date difference is probably not good either:

//Calculate Days Option Expiry
long DaysDifference    = EndOptionDate.getTime() - TodaysDate.getTime();

Will give you the difference in milliseconds between the two dates, not in days. You can use TimeUnit like this to obtain the day difference:

long dayDiff = TimeUnit.MILLISECONDS.toDays(EndOptionDate.getTime() - TodaysDate.getTime());
0

精彩评论

暂无评论...
验证码 换一张
取 消