I need to pass the date parameter to view in play framework
My controller looks like
something.render(new Date());
And on my view what I've done is
#(myDate : Date)
<script lang="text/javascript">
var time = "#(myDate)";
</script>>
This time variable I further need to use in jQuery.
Thing is Play framework is converting the date to string object.
What I want is date object itself.
If I remove the quotes around "#(myDate)" Java script gives following output.
var backupTimeString = 2015-01-15 00:01:28.767;
Uncaught Syntax Error : expecte number
I really need the object to passed as Date object not as String represnetation of Date
1) If you work in local time, you could pass the time as a formatted string :
something.render( ... new java.text.SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(new java.util.Date()) ...)
and convert it to javascript date in the view :
<script>
var t = new Date("#mydate");
</script>
According to http://dygraphs.com/date-formats.html the format aaaa/mm/jj hh:mm:ss is the most robust.
2) In case you don't work in local time, recent browsers accept ISO-8601 date with offset from UTC, for example :
new Date('2015-01-22T12:00-0600')
3) As a last resort, you can pass a timestamp :
something.render(... new java.util.Date().getTime() ...)
<script>
var t = new Date(#mydate);
</script>
Related
I'm setting values on a json that i want to send, but the value DateTime("2019-07-01T03:18:46Z") is not set on the json as i wanted to
I have tried to use the SimpleDateFormat, also with the DateTimeFormatter of the library joda but no one give me the result as a simple DateTime
EventDTO eventDTO = new EventDTO();
eventDTO
.withDescription("Description")
.withIssueType("Notificación CNS")
.withPriority("Medium")
.withProjectKey("10000")
.withIssueSubType("Apoderamiento de aeronave")
.withSummary("Summary")
.withRegion("LECM")
.withAirport("LEBB")
.withRaised(new DateTime("2019-07-01T03:18:46Z"));
when(jiraService.create(eventDTO)).thenReturn(successResponseDTO);
String json = new ObjectMapper().writeValueAsString(eventDTO);
the actual result that I'm getting is :
{"monthOfYear":7,"minuteOfHour":18,"hourOfDay":5,"yearOfEra":2019,"weekyear":2019,"centuryOfEra":20,"yearOfCentury":19,"millisOfDay":19126000,"secondOfDay":19126,"minuteOfDay":318,"era":1,"dayOfMonth":1,"dayOfWeek":1,"dayOfYear":182,"year":2019,"secondOfMinute":46,"millisOfSecond":0,"weekOfWeekyear":27,"zone":{"fixed":false,"uncachedZone":{"cachable":true,"fixed":false,"id":"Europe/Paris"},"id":"Europe/Paris"},"millis":1561951126000,"chronology":{"zone":{"fixed":false,"uncachedZone":{"cachable":true,"fixed":false,"id":"Europe/Paris"},"id":"Europe/Paris"}},"afterNow":false,"beforeNow":true,"equalNow":false}
I expect to get just
2019-07-01T05:18:46.000+02:00
I get information about timezone in such string format.
(UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius
Is it somehow possible to parse it into some TimeZone object in Java with standard library or external one?
Depending how you want to use the TimeZone afterwards you might either create a custom one
String input = "(UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius";
// assuming the format is always fixed at the beginning
String timeZoneOffset = input.substring(4,10);
TimeZone timeZone = TimeZone.getTimeZone("GMT" + timeZoneOffset);
System.out.println("timeZone = " + timeZone);
output (line wrapped)
timeZone = sun.util.calendar.ZoneInfo[id="GMT+02:00",offset=7200000,dstSavings=0,\
useDaylight=false,transitions=0,lastRule=null]
You might get into trouble related to the daytime savings.
Or you create a lookup map with an entry for each offset (stripped down code snipped)
String input = "(UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius";
// assuming the format is always fixed at the beginning
String timeZoneOffset = input.substring(4,10);
// needs to be initialized somewhere
Map<String, TimeZone> timeZones = new HashMap<>();
// you need to add all offsets
timeZones.put("+02:00", TimeZone.getTimeZone("EET"));
System.out.println("timeZone lookup = " + timeZones.get(timeZoneOffset));
output (line wrapped)
timeZone lookup = sun.util.calendar.ZoneInfo[id="EET",offset=7200000,dstSavings=3600000,\
useDaylight=true,transitions=123,lastRule=java.util.SimpleTimeZone[id=EET,offset=7200000,\
dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,\
startDay=-,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,\
endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]
I am attempting to write an example JSP page for myself (very new to jsp), and have gone over an example to write one, but how do I get time to consistently update?
here is my snippet of code:
<body>
<%
java.text.DateFormat df = new java.text.SimpleDateFormat(
"HH:mm:ss:SS z MM/dd/yyyy");
Calendar cal = Calendar.getInstance();
%>
<h1>
Current Date and Time:
<%=df.format(cal.getTime())%>
</h1>
</body>
By the way i'm using a tomcat server to deploy this
function updateYourTime() {
var now = new Date(),
months = ['January', 'February', '...'];
time = now.getHours() + ':' + now.getMinutes(),
date = [now.getDate(),
months[now.getMonth()],
now.getFullYear()].join(' ');
document.getElementById('currentTime').innerHTML = [date, time].join(' / ');
setTimeout(updateYourTime, 1000);//This method will call for every second
}
updateYourTime(); // initial call
see here for details
<div id="currentTime"></time>
do you mean to show clock in your pages?
you can use java script.
here is an example
to show server clock in clients jsp use this javascripcode with java
Add a label where ever you want to show the server Time
<strong>Server Time : </strong><label id="timelable"></label>
And then add the following java script code at the end of the jsp inside the body tag
<script type="text/javascript">
var myVar = setInterval(function(){ myTimer() }, 1000);
var jsVar= <%=java.util.Calendar.getInstance().getTimeInMillis()%>;
var timeZoneOffset=<%=java.util.TimeZone.getDefault().getOffset(System.currentTimeMillis())%>;
jsVar=jsVar+timeZoneOffset;
function myTimer() {
jsVar=jsVar+1000;
var d = new Date(jsVar);
var t=d.toUTCString();
document.getElementById("timelable").innerHTML = t;
}
</script>
Thats it now you will see the server time running in you jsp.
I want to show the date coming from data base in the jsp page in dd/MM/yyyy format. Here is my code in javascript.
$(document).ready(function(){
<%
SDateDTO sDTO = (SDateDTO) request.getAttribute("sDTO");
if(null != scholAvailDTO){
System.out.println(scholAvailDTO.getEndDate());
%>
var end = <%=scholAvailDTO.getEndDate() %>;
$("#endDateId").val(end);
<%
}
%>
});
In console it is coming 27/04/2010 but in jsp it is getting populated like 0.0033582089552238806 which is actually division result of the date. Any help will be appreciated. Thanks, Amit
Well yes - your Javascript will presumably be rendered to the browser as:
var end = 27/04/2010;
If you want it to be in a string literal then you'll need to add the quotes yourself:
var end = "<%=scholAvailDTO.getEndDate() %>";
Note that you'll need to be confident that the value itself doesn't have quotes - or other values which aren't appropriate for JavaScript - in there. I suspect there may well be a better approach than the above.
If getEndDate() returns java.util.Date object then, use DateFormat for get the formatted string and then wrap the value in quotes as below:
<%DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");%>
var end = "<%=formatter.format(scholAvailDTO.getEndDate()) %>";
If getEndDate() returns java.lang.String then simply wrap the value in quotes as below:
var end = "<%=scholAvailDTO.getEndDate() %>";
I am using jquery Datatables plugin. I followed the link http://www.codeproject.com/Articles/190718/jQuery-DataTables-and-J2EE-web-application-integra
I am passing a Date in the JSON object to the datatables plugin. The format from webservice call is like
"Sat Jan 10 00:08:00 EST 2009"
, I need to strip off the time, EST and the day, I mean it should look something like
"Jan 10, 2009"
and the column is sorted on the server side. All I need is to strip off the data on the fly on the client side. I am still in the process of learning datatables plugin, I am not sure of implementing this. Experts please point me to the right direction.
[Edit] Since it looks like the date comes back from the server as a string then you're easiest solution is probably a regular expression. Try this:
function reformatDate(dateStr) {
var r = /^\w{3}\s+(\w{3})\s+(\d{1,2})\s+.*?(\d{4})$/
, m = (''+dateStr).match(r);
return (m) ? m[1]+' '+m[2]+', '+m[3] : dateStr;
}
[Original] Assuming you are working with an actual Date object and you don't want to incur the overhead of a proper JavaScript date wrangling library (such as the excellent Datejs) you could format the date like so:
var formatDate = (function() {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return function(dt) {
return months[dt.getMonth()] + ' ' + dt.getDate() + ', ' + dt.getFullYear();
};
})();
formatDate(new Date()); // => "Mar 15, 2012"
I'm not sure if you can do this with bServerSide set to true but you might be able to setup column definitions using the aoColumns option for the datatable and then apply column rendering via
fnRender: function (o, val) {
parse your date here...
return newDateString;
}