Pages

Wednesday, 20 February 2013

Get Timestamp for the Present Day in Java

// Snippet for getting timestamp in milliseconds for the present day with time taken as 00:00:00
// Eg: ts for '2013-02-19'
public long getTsForToday() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(cal.YEAR);
    int month = cal.get(cal.MONTH);
    int date = cal.get(cal.DATE);
    cal.clear();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DATE, date);
    return cal.getTimeInMillis();
}

Tuesday, 19 February 2013

Access Service Class From src/groovy Classes

The groovy classes placed in the src/groovy folder does not have dependency injection by default. However we can can access service classes using servlet context.
package com.example.ExampleClass

import org.codehaus.groovy.grails.web.context.ServletContextHolder
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes

class ExampleClass {
    def context = ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)
    def exampleService = context.exampleService
}