I am using JodaTime2 library to create a date object with a given timezone as follow:
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
DateTimeZone tz = DateTimeZone.forID("America/New_York");
System.out.println("timezone=" + tz);
Date d = new DateTime(2013, 1, 1, 0, 0, tz).toDate();
System.out.println("Cur Date = " + d);
However when I print this date, the timezone reported is CST. What am I missing ?
timezone=America/New_York
Cur Date = Tue Jan 01 13:00:00 CST 2013
You're printing out the value of a Date object. Date doesn't have a time zone - Date.toString() always just uses the "default" time zone. A Date is just a number of milliseconds since the Unix epoch; it doesn't know about calendars or time zones.
You should either just stick within the Joda Time world, or (if you must) use a SimpleDateFormatter to convert a Date to a String - you can set the time zone on the formatter.
Related
I am trying to split a flowfile into multiple flow files on the basis of adding a month to a date which i am getting in the coming flowfile.
eg.
{"to":"2019-12-31T00:00:00Z","from":"2019-03-19T15:36:48Z"}
be the dates i am getting in a flowfile . so i have to split this single flow file into 11 flowfiles with date ranges like
{"to":"2019-04-19","from":"2019-03-19"}
{"to":"2019-05-19","from":"2019-04-19"}
{"to":"2019-06-19","from":"2019-05-19"}
....... and so till
{"to":"2019-12-31","from":"2019-12-19"} .
i have been trying with example inputs to split files with this into day wise flowfiles:
`
begin = '2018-02-15'
end = '2018-04-23'
dt_start = datetime.strptime(begin, '%Y-%m-%d')
dt_end = datetime.strptime(end, '%Y-%m-%d')
one_day = timedelta(days = 1)
start_dates = [dt_start]
end_dates = []
today = dt_start
while today <= dt_end:
tomorrow = today + one_day
print(tomorrow)
`
but i get a error in my Execute script processor. nifi flow screenshot
Since you're using Jython, you may have to cast today to some Jython/Python time variable or call today.getTime() in order to do arithmetic operations on it.
Does anyone of you have suggestions how to convert the Unix timestamp to ABAP MEZ/MESZ time and date?
The following code is from the ABAP-Reference, the code is for timestamps with lenght 15 or 21 but the Unix timestamp is currently 10 digits long.
DATA: time_stamp TYPE timestamp,
tz TYPE ttzz-tzone.
tz = 'MESZ'.
time_stamp = 15319830890000.
CONVERT TIME STAMP time_stamp TIME ZONE tz
INTO DATE DATA(dat) TIME DATA(tim)
DAYLIGHT SAVING TIME DATA(dst).
cl_demo_output=>write( |{ dat DATE = ISO } {
tim TIME = ISO } { dst }| ).
time_stamp = 15319830890000.
CONVERT TIME STAMP time_stamp TIME ZONE tz
INTO DATE dat TIME tim
DAYLIGHT SAVING TIME dst.
cl_demo_output=>write( |{ dat DATE = ISO } {
tim TIME = ISO } { dst }| ).
cl_demo_output=>display( ).
I solved the problem with this code. I am using now 13 digits epoche...
data: i(20) type n.
data: d type sy-datum."
data: t type sy-uzeit.
data: epoche type int8.
data: test type int8.
data: test2 type int8.
epoche = 1522836000000.
i = epoche / 1000.
d = '19700101'.
d = d + i div 86400.
t = i mod 86400.
write: d, t.
If you want to convert using a SAP time zone, this one can work too (no time to convert it to ABAP Objects):
FORM unixtime_2_date_time
USING
i_unixtime TYPE numeric
i_timezone TYPE timezone
CHANGING
e_date TYPE d
e_time TYPE t.
DATA l_tstmp_unix_era TYPE TZNTSTMPL.
DATA l_tstmp TYPE TZNTSTMPL.
CONSTANTS utc TYPE timezone value IS INITIAL.
CONVERT DATE '19700101' TIME '000000' INTO TIME STAMP l_tstmp_unix_era TIME ZONE utc.
l_tstmp = CL_ABAP_TSTMP=>add( tstmp = l_tstmp_unix_era secs = i_unixtime ).
CONVERT TIME STAMP l_tstmp TIME ZONE i_TIMEZONE INTO DATE e_date TIME e_time .
ENDFORM.
You can use cl_pco_utility Java utility class for that, particularly method convert_java_timestamp_to_abap. It perfectly accepts 10-character Unix time.
CLASS zcl_epoch DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
unix_time_to_timestamp
IMPORTING iv_timestamp TYPE timestamp.
ENDCLASS.
CLASS zcl_epoch IMPLEMENTATION.
METHOD unix_time_to_timestamp.
CONSTANTS: c_tzone TYPE ttzz-tzone VALUE 'CET'.
DATA: lv_timestamp_msec TYPE string,
lv_timestamp TYPE timestamp,
lv_date TYPE datum,
lv_time TYPE uzeit.
lv_timestamp_msec = iv_timestamp * 1000.
cl_pco_utility=>convert_java_timestamp_to_abap(
EXPORTING
iv_timestamp = lv_timestamp_msec
IMPORTING
ev_date = lv_date
ev_time = lv_time
).
lv_timestamp = lv_date && lv_time.
CONVERT TIME STAMP lv_timestamp TIME ZONE c_tzone INTO DATE lv_date TIME lv_time.
cl_demo_output=>display( |{ lv_date DATE = ISO } { lv_time TIME = ISO }| ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
zcl_epoch=>unix_time_to_timestamp( '1532196799' ).
It worth mentioning that MEZ/MSEZ are NOT standard abbreviations and should not be used. For example, MEZ is the same as CET. Look at list of common time zones here or here.
Also, it's better to stick to list of available time zones of your system, which is table TTZZ.
Just add the epoch time to '19700101' date with initial time.
DATA lv_initial_timestamp TYPE timestamp.
CONVERT DATE '19700101' TIME '000000' INTO TIME STAMP lv_initial_timestamp TIME ZONE sy-zonlo.
ev_timestamp = cl_abap_tstmp=>add( tstmp = lv_initial_timestamp secs = iv_epoch_timestamp ).
CONVERT TIME STAMP ev_timestamp TIME ZONE sy-zonlo INTO DATE ev_date TIME ev_time.
For MEZ/MESZ you can choose time zone accordingly.
I'm receiving timestamps in the following format '2016-08-17T14:00:00-04:00', which I can parse in moment with moment('2016-08-17T14:00:00-04:00', 'YYYY-MM-DDTHH:mm:ssZ').
But the problem is that I want to print out .format('LLLL') and have it read Wednesday, August 17, 2016 10:00 AM, i.e. subtracting -04:00 from 14:00:00 (NY from UTC). It appears that there is a _tzm: -240 property in the moment object that looks like it holds that -4 hours value, but how do I use that property?
The other goal is to be able to pass in the current time and test if it is between the startDate and endDate variables below. I am guessing if I can convert both to NY-EST I can do this, but I can't seem to get moment to accept the timezone parameter.
Any thoughts?
var moment = require('moment');
// Timestamp strings from API
var startDate = '2016-08-17T14:00:00-04:00';
var endDate = '2016-08-17T15:00:00-04:00';
// Create a range from the start and end dates
var range = moment().range(new Date(startDate), new Date(endDate));
// Get the current time
var currentTime = new Date();
// Does the current time fall within the range b/w the start and end dates
range.contains(currentTime);
A solution I found is below. Adding the value from momentObj._tzm to the parsed date.
module.exports.convertDateToProperTimezone = function (dt) {
var _m = moment(dt, 'YYYY-MM-DDTHH:mm:ssZ');
return _m.add(_m._tzm, 'minutes');
};
I have a date value as Fri Feb 15 19:43:05 EST 2013
I could convert it into 2013-02-15 07:43:05 as String.
Now i need to get the date object of this String. I tried using simpleDateFormat in Groovy/Grails but it would just return the original value: Fri Feb 15 07:43:05 EST 2013
The way I am doing it is:
String dateCreated = dateValue.format("yyyy-MM-dd hh:mm:ss")
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //required format
Date newDate = sdf.parse(dateCreated)
Can anyone help me?
If you want dates without Time Zone I suggest you to look the Joda-Time API. There's a plugin for Grails.
Look at LocalDate and LocalDateTime, you can construct them passing separated values (year, month, day, hour, second).
If you have a Date instance, you just need:
Date dateValue = ...
String dateCreated = dateValue.format("yyyy-MM-dd hh:mm:ss")
and dateCreated will contain value like 2013-02-15 07:43:05. You don't need to do anything else
I have one variable
Dim tt="2008-10-20 10:00:00.0000000"
I want to change it into date,
Try CDATE(tt) see http://www.w3schools.com/vbscript/func_cdate.asp. I used
vbscript cdate
as keywords at Google. There were more results.
Edit: Based on the comment below (I'm sorry for mixing up), using
FormatDateTime(date,format)
Format contains following constants:
0 = vbGeneralDate - Default. Returns date: mm/dd/yy and time if
specified: hh:mm:ss PM/AM.
1 = vbLongDate - Returns date: weekday, monthname, year
2 = vbShortDate - Returns date: mm/dd/yy
3 = vbLongTime - Returns time: hh:mm:ss PM/AM
4 = vbShortTime - Return time: hh:mm
(copied from http://www.w3schools.com/vbscript/func_formatdatetime.asp)
This link, (MS CDate page), explains that:
adate = CDate(astring)
converts a string into a date object. For there, you can format it with the FormatDateTime function
str = FormatDateTime(Date)
the FormatDateTime function is "smart" -- it will format as date and time if both are present, otherwise it will format with whichever of date or time is present.
I propose a safe solution which returns the result only if the conversion is successful:
s="2008-10-20 10:00:00.0000000"
On Error Resume Next
d=CDate(Left(s,19))
On Error Goto 0
if not IsEmpty(d) then MsgBox d
Try it for a non-valid date or non-valid format. The result will be empty.
s="2008-02-31 10:00:00"
In same contexts, it is necessary to initialize the variable collecting result of CData. I recommend to initialize it as Empty. Example below shows such case - counting valid dates in a string array:
Lines = array("2008-10-20 10:00:00.0000000", "2008-10-20 10:00:00", "", "2008-02-31", "Today", "2017-02-7")
On Error Resume Next
Count=0
for each Line in Lines
d=Empty
d=CDate(Line)
if not IsEmpty(d) then Count=Count+1
next
On Error Goto 0
MsgBox "Number of valid dates is "&Count
The correct answer is 2. Without initialization we get 5 as the CDate does not do anything on error so variable keeps the value from a recent iteration in the loop.
If do not need your milliseconds, your could use the following:
<script type="text/vbscript">
s="2008-10-20 10:00:00.0000000"
arr= Split(s, ".")
d=CDate(arr(0))
document.write(d)
</script>
I believe cdate is dependent on local settings to parse the string. This is no good in many situations.
To avoid this you need to use
DateSerial()
and if needed add any time components to the result separately.
The date literal in classic asp is unreliable. If the first or second part is greater than 12, it takes that value for day, the other as month. If both parts are less than 12, the interpretation is unpredictable: sometimes american and sometimes british.
A work-around is to force the entry of dates into separate fields or use a date entry module which can set the date into british or american style.
A date literal should be treated as american and use a function to convert that into a date variable using Dateserial().
function amerdate(str)
'str 3/5/2023 form: in american format: use for calculations including date
Dim d
d = Split(str, "/")
amerdate = Dateserial(d(2), d(0), d(1))
end function
Use only american date in calculations like Dateadd() etc.
someday = "3/5/2023" '5th march, american date literal
nextday = Dateadd("d", 1, amerdate(someday))
Whenever a date display is required, convert to british date and show it.
function britdate(str)
'str: 3/5/2023 form: display in british form. not for calculations
Dim d
d= Split(str, "/")
britdate = d(1) & "/" & d(0) & "/" & d(2)
end function