Beanshell How do I convert a string to a datetime - beanshell

I'm using beanshell and I need to convert a string in the format '2012.08.14 07:30:00.000' to date time integer in the format 1344925800000
Does anyone have any ideas?
Thanks in advance
Al

I hope that your example result is only an example, because using Java means I obtain a different result. But a standard is to use the number of milliseconds since January 1, 1970, 00:00:00 GMT.
I don't know if there is a more generic way, that would allow you to specify a format string. Here I use a string conversion and a format for specific locale.
import java.text.DateFormat;
sDate = "2012.08.14 07:30:00.000";
// replace beginning dots with hyphens
sDate = sDate.replaceFirst("([0-9]{4})\\.([0-9]{2})\\.([0-9]{2}) ", "$1-$2-$3 ");
// use a date format from a specified locale
formatter = DateFormat.getDateInstance(
DateFormat.MEDIUM,
new Locale("PL"));
javax.swing.JOptionPane.showMessageDialog(null,
"time=" + formatter.parse(sDate).getTime());

Related

java.sql.SQLException: ORA-01843: not a valid month error - date format conversion issue

I'm having a strange issue here with a query from which I'm trying to pull data queried by start and end date parameters.
I'm conducting the following.
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String preparedQuery = "SELECT ID, FULNAME, FRMEMAIL, LOCATION,TYPE1,TYPE2,BORROWER_NAME,LOAN_NUM,TIME_REQ
FROM FORM_REDUCVU WHERE to_date(TIME_REQ,'yyyy-mm-dd') >= ? AND to_date (TIME_REQ,'yyyy-mm-dd') <= ? ORDER BY ID DESC";
java.util.Date util_StartDate = sdf.parse( request.getParameter("time_req_date") );
java.sql.Date timreq_datevar1 = new java.sql.Date( util_StartDate.getTime() );
java.util.Date util_EndDate = sdf.parse( request.getParameter("time_req_date2") );
java.sql.Date timreq_datevar2 = new java.sql.Date( util_EndDate.getTime() );
out.println("datestamp java time vals "+timreq_datevar1 + " " + timreq_datevar2);
PreparedStatement prepstmt = connection.prepareStatement(preparedQuery);
prepstmt.setDate(1, timreq_datevar1);
prepstmt.setDate(2, timreq_datevar2);
And the out.print values will give something like 2018-10-09 and 2019-02-20.
This does match the date picker selections I'm making, which are in mm/dd/yyyy format, i.e. 10/09/2018 and 02/20/2019.
Now the above returns no errors in the log, but no data either. But I did have the SQL query as "
WHERE to_datechar(TIME_REQ,'mm/dd/yyyy') >= ? AND to_char(TIME_REQ,'mm/dd/yyyy') <= ?
When changing to this above, I get the error in the topic of not a valid month.
I would have thought the SimpleDateFormat class would have parsed the date to mm/dd/yyyy in pattern, but it seems to make the java.slq.Date object pattern hyphenated, like yyyy-mm-dd.
Am I not parsing it correctly? Or is this going to require a different class or package, like java.Time? I would thought this would work, but there has to be some small conversion method not working.
Bottom line - I'd really prefer to keep the jQuery date pickers with their mm/dd/yyyy formats in tact, and be able to query by these entries.
Any input regarding this is welcomed.
Thank you!
Do NOT use to_date() on a column that is alread a DATE to_date() will convert the date to a varchar just to convert it back to a DATE which it was to begin with.
So your condition should be:
WHERE TIME_REQ >= ? AND TIME_REQ <= ?
You are correctly using setDate() so Oracle performs a date to date comparison - there is no need to convert the date in the database to a string value.
Ideally you should use java.time.LocalDate and setObject() instead of java.sql.Date though (however not all Oracle driver versions support that)

Convert string date to show 2 year digits

i have a string which has a value of "08-06-2008". I want the result to look like "08-06-08". Is there a way to do this? I've tried CDate but that only gives me 8/06/2008 which doesnt resolve the issue.
Parse it to Date and back to string:
Dim dt As Date = Date.ParseExact("08-06-2008", "MM-dd-yyyy", CultureInfo.InvariantCulture)
Dim result As String = dt.ToString("MM-dd-yy", CultureInfo.InvariantCulture)
Since that is a normal format you could also omit the format string and use Date.Parse directly:
Dim dt As Date = Date.Parse("08-06-2008", CultureInfo.InvariantCulture)
I have used CultureInfo.InvariantCulture to avoid localization issues, normally your current culture is used in Parse/ParseExact and ToString.
See: Custom Date and Time Format Strings
Firstly, avoid CDate. It is a hangover from VB6, and is almost certainly less efficient than using the .net equivalent.
The following should give you the right answer:
string value = DateTime.ParseExact("08-06-2008", "dd-MM-yyyy").ToString("dd-MM-yy")
Note that your existing date format is ambiguous, so I've been unable to tell if you're meaning the 6th of August or the 8th of June - this is why I've used ParseExact over Parse. ParseExact is also slightly more efficient, as it tells the framework which format to use, rather than it having to guess.
Try this
Dim FormatString As String = ""
Dim SampleDate As DateTime
SampleDate = Now()
FormatString = Format(SampleDate,"dd-MM-yy")

vb.NET (for ASPX page): Parse a string to a date

I'm trying to make this line work:
MyDateTime = DateTime.ParseExact("2/8/2013 11:59:00 AM", "yyyy-mm-dd hh:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)
I'm getting date/time strings from spreadsheets in the above format, and I can't control that. There's a ton of help online, including this site, about converting strings to dates, and I've tried them all, but I keep getting this error :
"System.FormatException: String was not recognized as a valid DateTime."
I"m just about ready to write my own custom parser, but that doesn't seem very elegant. Is there some built-in way to convert a string like mine into the date/time format I need?
Thanks for any help.
Your format string is wrong. You're entering a date in d/M/yyyy hh:mm:ss tt format, but telling it to expect a date in yyyy-mm-dd hh:mm:ss format. The two do not match exactly, so DateTime.ParseExact is quite rightly throwing an exception at you.
Try:
MyDateTime = DateTime.ParseExact("2/8/2013 11:59:00 AM", "d/M/yyyy hh:mm:ss tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)
This tells it to expect the following characters:
Integer from 1 through 31 (depending on the length of the month)
/ character
Integer from 1 through 12
/ character
4 digit year
Space
Integer from 1 through 12
: character
Integer from 00 through 59
: character
Integer from 00 through 59
Space
Two character meridian specifier ("AM" or "PM")
For more info on the datetime format strings, check out this MSDN page
I think you need to change the string format to match what you are passing in. You seem to be passing in something more like this:
"d/M/yyyy hh:mm:ss"
Give that a try and see how it works. Note that you need to use MM for months--'mm' is used for minutes.

Vb.net DateTime conversion from String

I am getting this datetime format in an xml file:
2012-06-14T11:15:41.587-07:00
2012-06-14T10:49:32.397-07:00
2012-06-11T11:10:49.2-07:00
I believe I understand some of it, please correct me if I am wrong:
2012-06-14 = date
T = time identifier
10:49:32.397 = hour min second milliseconds
-07:00 = I have no idea
I need to convert this to something I can sort on for my datagrid view.
When I try something like this:
Console.WriteLine(String.Format("{0:d/M/yyyy HH:mm:ss}", "2012-06-14T10:49:32.397-07:00"))
I'm getting the original string back out with no conversion.
Anyone have any suggestions?
-07:00 = I have no idea
-7 is a timezone offset. It means the DateTime is 7 hours behind UTC, which would indicate the US Mountain Time.
You want to use DateTime.Parse to get a DateTime object.
Dim val As String = "2012-06-11T11:10:49.2-07:00"
Dim parsedDateTime As DateTime = DateTime.Parse(val)
'Do whatever with parsedDateTime here
Take a look at this article:
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
It should give you a better grasp of how to use the DateTime object. The time you have is the ISO 8601 format.
The -07:00 is the offset from UTC. You can parse this via DateTimeOffset.ParseExact, which includes a specifier for the offset ("zzz" format specifier).
In your case, I believe this would be :
Dim xmlValue = "2012-06-14T11:15:41.587-07:00"
Dim value as DateTimeOffset = DateTimeOffset.ParseExact(xmlValue, "yyyy-MM-dd\Thh:mm:ss.fffzzz", CultureInfo.InvariantCulture)

Converting Date string starts with days to DateTime Format vb.net

I Have this date "27/03/1985" and because it starts with days i can't convert it to datetime.
See: http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
DateTime.ParseExact() lets you specify the format of the string representation.
Yes you can. You just need to use the right culture specifier. It's a UK date so you need so supply the correct format provider:
dateValue = DateTime.Parse(dateString, culture);
where culture is of type CultureInfo
Source