Skip to content

Instantly share code, notes, and snippets.

public final class DateOperations {
/**
* Gets the Epoch seconds of the given LocalDateTime
* @return long of the seconds since the beginning of time (for Java)
*/
public static long getLocalSeconds(LocalDateTime localDateTime) {
ZoneId zone = ZoneId.systemDefault();
ZonedDateTime zdt = localDateTime.atZone(zone);
return zdt.toEpochSecond();
}
import javax.annotation.Nonnull;
import java.util.Collection;
/**
* Utility class for setters of collections in objects.
*
* <pre>
* {@code
* private Set<String> items = new HashSet<>();
* public void setItems(Collection<String> items) {
* CollectionSetter.refill(this.items, items);
@benkn
benkn / cookieUserInfo.js
Created April 16, 2015 20:08
Ask users to enter their name and store it in a cookie.
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) {
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
}
function getCookie (name) {
/* Truncates value within a specified width and appends with ... */
.truncate {
/*width: 100%;*/ /*optionally set width here, but more useful not set */
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
@benkn
benkn / QueryParam.js
Created July 26, 2014 22:00
Get a query param from the current page url
/* Get a parameter from the url */
function getVarFromURL(varName){
var url = window.location.href;
url = url.substring(url.indexOf('?'));
var urlLowerCase = url.toLowerCase();
varName = varName.toLowerCase();
if (urlLowerCase.indexOf(varName + "=") != -1) {
var value = url.substring(urlLowerCase.indexOf(varName) + varName.length + 1);
if (value.indexOf('&') != -1) {
value = value.substring(0, value.indexOf('&'));
@benkn
benkn / DateFunctions.js
Created July 13, 2014 22:55
Date functions in Javascript
var DateUtil = {
/* Get the date for yesterday in YYYY-MM-DD format */
getYesterdayYMD : function() {
var d = new Date();
var m = d.getMonth() + 1;
var n = d.getDate() - 1;
var ds = d.getFullYear() + "-" + (m< 10? '0' + m : m) + "-" + (n<10 ? '0' + n : n);
return ds;
},
/* Get the date for today in YYYY-MM-DD format */
@benkn
benkn / HashUtil.java
Created January 22, 2013 19:28
Util class to create hash codes (SHA-1 in this example) for a given String.
import java.security.MessageDigest;
public class HashUtil {
/** Change to SHA-256 or MD5 if needed */
private static final String HASHING_ALGORITHM = "MD5";
/**
* The usual default character encoding, but hard defining it to avoid
* complications.
@benkn
benkn / HasValue.java
Last active December 11, 2015 11:58
Check if the given String is not null or has a value other than whitespace.
/**
* Check that the given String is not null and has a value other than
* whitespace.
*
* {@code
* hasValue(null) = false
* hasValue("") = false
* hasValue(" ") = false
* hasValue("bob") = true
* hasValue(" bob ") = true