Freemarker get current timestamp - java

How can I get the current timestamp (number of seconds since 01/01/1970) in a freemarker file? I know the .now var but I didn't figure how to get a basic timestamp format.

Use the ?long operator:
.now?long
for milliseconds, or
${.now?long / 1000}
for seconds

Related

Convert Java Timestamp in TOD Format (Time of Day) on z/os

As the Search Method "readSearch" of the ZLogstream class needs the Timestamp in TOD format, I've to convert the JAVA Timestamp in TOD (Time of Date) format.
I couldn't find any class (wrapper), which does that for me. Only the getTodClock of the ZUtil class give me the current TOD (Time of Date),but I couldn't convert any timestamp in the right format.
Could anybody help me?
Thanks in advance.
This works for me in a Rexx procedure:
Tod = (epoc*4096000000)+9048018124800000000
TOD (Time-of-Day in S360, S370, S390 & z/Arch IBM's archs) is a 64bit counter whose Bit position 31 is incremented every 1.048576 seconds, starting at 1900-01-01.
So, 2**32/1.048576 = 4096000000 = 1 sec.
9048... is the difference between 1900-01-01 and epoch 1970-01-01 in TOD units.

Hibernate Envers Timestamp from revision date

I'm using MySQL and Hibernate Envers to log audity info, it saves a timestamp in a numeric format, how can i convert this timestamp to date in MySQL, not in Java?
Thanks!
UPDATE -
You can use FROM_UNIXTIME() in-built function.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
Simply taking it into Date object, it should work -
Date date = resultSet.getTimestamp("timestamp");
I guess this timestamp includes milliseconds, so you can simply divide this value by 1000 :
FROM_UNIXTIME(hr.`TIMESTAMP`/1000)
Formatted:
DATE(FROM_UNIXTIME(hr.`TIMESTAMP`/1000))
OR
DATE_FORMAT(FROM_UNIXTIME(hr.`TIMESTAMP`/1000) ,'%Y-%m-%d %H:%i:%s')

insert and extract hour an minute from oracle

I work with oracle
I want to insert data which contains hour and minute
I have this column : DATE_ARCH
the type of this column is date
I have this java code which should insert date in this column
transfers.setDateArch(new java.sql.Date(
System.currentTimeMillis()));
but when I try to extract hour and minute from DATE_ARCH
using this sql code :
select to_char(transfers.DATE_ARCH , 'HH:MM') from transfers where id_transfer='TR-300'
I have all time this value :
12:05
Updated :
I try with this code :
Timestamp t = new Timestamp(new Date().getTime());
transfers.setDateArch(t);
I try to extract hour and minute using this code :
select to_char(transfers.DATE_ARCH , 'HH24:MI') from transfers where id_transfer='TR-258'
but I have in all case this value :
00:00
as I already said the type of DATE_ARCH is date
When I try in sql with :
UPDATE transfers SET DATE_ARCH = SYSDATE
I have the correct value using
select to_char(transfers.DATE_ARCH , 'HH24:MI') from transfers where id_transfer='TR-258'
now I want to know how can I insert date with hour and minute using java code
You are correct. I do not know enough about the java interface to Oracle to know the right solution. But, your solution is inserting the date with no time. The expression:
to_char(transfers.DATE_ARCH , 'HH:MM')
is returning "12" because that is midnight and "05" because it is May. The correct expression for minutes is:
to_char(transfers.DATE_ARCH , 'HH:MI')
and for a 24-hour clock:
to_char(transfers.DATE_ARCH , 'HH24:MI')
I do not, alas, know how to fix the java code. But perhaps there is a DateTime method that you can use.

Modify date without modifying time

With JodaTime, without using the 'plus' or 'minus' functions and using the least lines of code, how can I set a new date without modifying the time?
My first attempt was to store the 'time' parts of the DateTime in separate ints using getHoursOfDay() and getMinutesOfHour() etc - then create a new DateTime with the required date and set the hours, minutes, and seconds back again. But this method is pretty clunky, and I was wondering if there was a less verbose method for doing this - ideally with just one line of code.
For example:
22/05/2013 13:40:02 >>>> 30/08/2014 13:40:02
Is JodaTime a must? Basic way to do this is
1. extract just time from timestamp.
2. add this to just date
long timestamp = System.currentTimeMillis(); //OK we have some timestamp
long justTime = timestamp % 1000 * 60 * 60 * 24;// just tiem contains just time part
long newTimestamp = getDateFromSomeSource();//now we have date from some source
justNewDate = newTimestamp - (newTimestamp % 1000 * 60 * 60 * 24);//extract just date
result = justNewDate + justTime;
Something like this.
Previously accepted answer were removed by moderator, as it contains only link to javadoc.
Here is edited version.
You could do it like this
DateTime myDate = ...
myDate.withDate(desiredYear, desiredMonth, desiredDayOfMonth);
JavaDoc is here: DateTime.withDate(int year, int month, int dayOfMonth)
use withFields like this:
new DateTime().withFields(new LocalDate(2000,1,1))
This will set all date time fields of the DateTime to those that are contained in the LocalDate - year, month and day in this case. This will work with any ReadablePartial implementation like YearMonth for example.

Difference in Timestamp in PHP and Java

I try to generate timestamp in PHP and Java(Android). Both needs to be same. But I am getting a difference of 3000+ between the time stamps. Please check the code below!
JAVA :
long unixTime = System.currentTimeMillis() / 1000L;
Php :
$time2 = time();
System.currentTimeMillis() will get me the current time in mobile and java.util.Date(); gets me based on GMT. Solved it.

Categories