How to convert a string into TimeSeriesDataItem - java

I am using Jfreechart. I have the following code:
TimeSeries t1 = new TimeSeries("EUR/GBP");
t1.add(new TimeSeriesDataItem....);
But my SQL query gives date in String format & value in Double. I want to use TimeSeriesDataItem. Please let me know how to convert my String into TimeSeriesDataItem.
Please let me know how to add my Double value to TimeSeriesDataItem.
Thanks in Advance.

1) convert your date from String to java.util.Date
2) wrap this Date instance using one of the classes extending RegularTimePeriod.
eg. RegularTimePeriod p = new Day (myDate)
3) TimeSeriesDataItem t = new TimeSeriesDataItem (p, a_numeric_value)

What is the format of the date string?
Assuming the format is DD-MM-YY.
First convert the string to a Date object.
String date_S = "04-06-16"; //your date from SQL
Date date;
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-yy");
try {
date = sdf2.parse(dateS);
} catch (ParseException e) {
e.printStackTrace();
}
TimeSeries add takes RegularTimePeriod and Double as arguments
So create a RegularTimePeriod object and add it to the series.
RegularTimePeriod rtp = new Date(date);
TimeSeries t1 = new TimeSeries("EUR/GBP");
TimeSeriesDataItem tsdi = new TimeSeriesDataItem(rtp , Double);
t1.add(tsdi);

Related

How to convert String to Date (Unparseable date: "03062019")

The question is not a duplicate, do not confuse it. It addresses mainly how you cannot store the variable of type String in Date, but you can format it after casting it. The other answer marked as "duplicate" is vage and not related...
I'm trying to convert a String X = ""01021990"; to a data type Date, but everytime I store it, it gives me an error that cannot be cast
This is what works:
String date = "01062014";
DateFormat dateInput = new SimpleDateFormat("MMddyyyy");
DateFormat desireDate = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(desireDate.format((Date)dateInput.parse(date)));
This is what I'm trying to do that doesn't work:
DateFormat inputDate = new SimpleDateFormat("MMddyyyy");
DateFormat desireDateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date xs = desireDateFormat.format((Date)inputDate.parse(date));
However if I just print it, it does works:
System.out.println(desireDateFormat.format((Date)inputDate.parse(date)));
But I'm trying to store it into the data type Date so I can pass it to a constructor.
Thank you
desireDateFormat.format((Date)inputDate.parse(date))
returns String. Of course, you can println that but cannot cast String to Date This would work: Date xs = inputDate.parse(date);

How to format date/time string? (Java)

Hi can anyone help please? I am trying to format a date and time string.
Currently it looks like this "20160112T110000Z" and I need it to be "2016-01-12T11:00:00Z"
The string without the special characters are returned from a 3rd party recurrence library. I need to convert it to have the special characters before parsing it to a Calendar object.
Can anyone help please?
The code that I have so far looks like:
final String TIMEFORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
String string = "20160112T110000Z";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date = format.parse(string);
System.out.println(date);
However this just does not work.
Any suggestions are appreciated
You have to read the string with a format matching the source, this gives you a correct Date.
Then simply write it with the format you want :
String string = "20160112T110000Z";
String originalStringFormat = "yyyyMMdd'T'HHmmss'Z'";
String desiredStringFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";
SimpleDateFormat readingFormat = new SimpleDateFormat(originalStringFormat);
SimpleDateFormat outputFormat = new SimpleDateFormat(desiredStringFormat);
try {
Date date = readingFormat.parse(string);
System.out.println(outputFormat.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
try this
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
// you can change format of date
Date date = formatter.parse(strDate);
Timestamp timeStampDate = new Timestamp(date.getTime());
return timeStampDate;

How to converted timestamp string to a date in java

I have a string "1427241600000" and I want it converted to "yyyy-MM-dd" format.
I have tried, but I am not able to parse it, please review the below code
try {
String str = "1427241600000";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date date =sf.parse(str);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
I would like to know where I went wrong.
You should try it the other way around. First get the Date out of the milliTime and then format it.
String str = "1427241600000";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(Long.parseLong(str));
System.out.println(sf.format(date));
the conversion is highly dependent on what format the timestamp is in. But i assume the whole thing should actually be a long and is simply the systemtime from when the timestamp was created. So this should work:
String str = ...;
Date date = new Date(Long.parseLong(str));
Use Date date =new Date(Long.parseLong(str)); to convert your String to Date object.
if you are using SimpleDateFormat() the format specified as a parameter to this function should match the format of the date in the String (str in your case). In your case yyyy-MM-dd does not match the format of the time stamp (1427241600000).
You can do it like this:
use a SimpleDateFormat with an appropriate format string (be careful to use the correct format letters, uppercase and lowercase have different meanings!).
DateFormat format = new SimpleDateFormat("MMddyyHHmmss");
Date date = format.parse("022310141505");

convert String to TIMESTAMP fo inserting time in table?

I created a table :
create table Appointment(
App_ID number primary key,
Doctor_ID number,
Patient_ID number,
App_Date Date,
App_Time TIMESTAMP,
App_Charges number);
i know how to convert String to java.sql.Date.
But for time i'm doing
String s1=time.getSelelctedItem().toString();//specifying time from a combo box
and then
st.setString(5,s1);
Please tell me the changes i need to make..
Thanks.
I'd use a java.util.Date in the middle:
// Specifying time from a combo box
String s1 = time.getSelelctedItem().toString();
// Convert String to Date according to the format
SimpleDateFormat df = new SimpleDateFormat("hh:mm");
Date date = df.parse(s1);
// Convert Date to Timestamp
Timestamp ts = new Timestamp(date.getTime());
// Set it:
st.setTimestamp(5, ts);
check the below code :
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "29/03/2014";
try {
Date date = formatter.parse(dateInString);
java.sql.Timestamp timest = new java.sql.Timestamp(date.getTime());
System.out.println(timest.getDate());
} catch (Exception e) {
e.printStackTrace();
}
so timest is the timestamp got from date which is parsed from datestring "29/03/2014" using SimpleDateFormat.

How to convert a string to RegularTimePeriod in java?

I am using Jfreechart. I have the code like this:
TimeSeries t1 = new TimeSeries("EUR/GBP");
t1.add(new Day(4, MonthConstants.JANUARY, 2001), new Double(1.5807));
But I get String from my SQL query. TimeSeries accepts only RegularTimePeriod or TimeSeriesDataItem.
Please let me know how to convert a String into RegularTimePeriod.
Thanks in Advance.
First you can get a Date object by parsing your mysql date string using a SimpleDateFormat, then create your RegularTimePeriod using the constructor with a Date arg.
Basically (assuming mysqlDateStr is your string from mysql query) :
SimpleDateFormat standardDateFormat = new SimpleDateFormat("yyyy-MM-dd");
// (Define your formatter only once, then reuse)
Date myDate = standardDateFormat.parse(mysqlDateStr);
// (you may want to catch a ParseException)
t1.add(new Day(myDate), new Double(1.5807));

Categories