How can I parse a Unix timestamp to a date string in Kotlin?
For example 1532358895 to 2018-07-23T15:14:55Z
The following should work. It's just using the Java libraries for handling this:
val sdf = java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
val date = java.util.Date(1532358895 * 1000)
sdf.format(date)
Or with the new Time API:
java.time.format.DateTimeFormatter.ISO_INSTANT
.format(java.time.Instant.ofEpochSecond(1532358895))
Related
How can i compare Dates in Kotlin?
I have the Date of an Event as a String (Format:dd/mm/yy) and I want to check if it is within the next 7 Days of the current Date.
The time in this case is not relevant or if needet I would use midnight.
Can someone please help me with this?
In my current code i got both Dates by this:
val date = document.data["Date"].toString() //Example: 22/08/22 (dd/MM/yy)
val today = SimpleDateFormat("dd/MM/yy").format(Date()).toString()
this is within a Android environment.
I can't get the date more specific because i am getting it from a Database.
Parse string into LocalDate using its parse method. There’s no out-of-the-box DateTimeFormatter for dd/mm/yy format, but you can trivially create one using DateTimeFormatter.ofPattern.
Get current date using LocalDate.now().
diff = ChronoUnit.DAYS.between(now, date1)
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)
I'm new to Spark SQL and am trying to convert a string to a timestamp in a spark data frame. I have a string that looks like '2017-08-01T02:26:59.000Z' in a column called time_string
My code to convert this string to timestamp is
CAST (time_string AS Timestamp)
But this gives me a timestamp of 2017-07-31 19:26:59
Why is it changing the time? Is there a way to do this without changing the time?
Thanks for any help!
You could use unix_timestamp function to convert the utc formatted date to timestamp
val df2 = Seq(("a3fac", "2017-08-01T02:26:59.000Z")).toDF("id", "eventTime")
df2.withColumn("eventTime1", unix_timestamp($"eventTime", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").cast(TimestampType))
Output:
+-------------+---------------------+
|userid |eventTime |
+-------------+---------------------+
|a3fac |2017-08-01 02:26:59.0|
+-------------+---------------------+
Hope this helps!
Solution on Java
There are some Spark SQL functions which let you to play with the date format.
Conversion example : 20181224091530 -> 2018-12-24 09:15:30
Solution (Spark SQL statement) :
SELECT
...
to_timestamp(cast(DECIMAL_DATE as string),'yyyyMMddHHmmss') as `TIME STAMP DATE`,
...
FROM some_table
You can use the SQL statements by using an instance of org.apache.spark.sql.SparkSession. For example if you want to execute an sql statement, Spark provide the following solution:
...
// You have to create an instance of SparkSession
sparkSession.sql(sqlStatement);
...
Notes:
You have to convert the decimal to string and after you can achieve the parsing to timestamp format
You can play with the format the get however format you want...
In spark sql you can use to_timestamp and then format it as your requirement.
select
date_format(to_timestamp(,'yyyy/MM/dd HH:mm:ss'),"yyyy-MM-dd HH:mm:ss") as
from
Here 'timestamp' with value is 2019/02/23 12:00:00 and it is StringType column in 'event' table.
To convert into TimestampType apply to_timestamp(timestamp, 'yyyy/MM/dd HH:mm:ss). It is need to make sure the format for timestamp is same as your column value. Then you apply date_format to convert it as per your requirement.
> select date_format(to_timestamp(timestamp,'yyyy/MM/dd HH:mm:ss'),"yyyy-MM-dd HH:mm:ss") as timeStamp from event
How to Change the date format yyyy/MM/dd HH:mm:ss to dd_mm_yy through modified java script value in pentaho?
Use this function in Modified Java Script Value step:
date2str(input, "dd_MM_yy");
You can use the SELECT step, and then in the Meta Data Tab, you can select the datatype as DATE and then use the format as "dd_MM_yy"
That's it, pretty simple :)
In pentaho a Date has always the time. There is not such thing as only date.
So, my guess would be that you need to format on output, converting it to a string for example.
But, if what you need is the date without the time, and by this i mean "yyyy/mm/dd 00:00:00", you can get it like this in Javascript:
var dateFieldValue=<yourdatecolumn>;
var year = dateFieldValue.getYear()+1900;
var month = dateFieldValue.getMonth();
var day = dateFieldValue.getDate();
var dateOnly = new Date(year,month,day);
or the last line could be:
var dateOnly = year + '-' + (month+1) + '-' + day;
if you want a String output like 'yyyy-mm-dd'
Hope this helps.
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());