Skip to content

Instantly share code, notes, and snippets.

@lutzhorn
lutzhorn / Camel2Snake.java
Created Mar 26, 2019
Convert between camelCase and snake_case using Guava in Java
View Camel2Snake.java
CaseFormat camelCase = CaseFormat.LOWER_CAMEL;
CaseFormat snakeCase = CaseFormat.LOWER_UNDERSCORE;
Converter<String, String> camelToSnake = camelCase.converterTo(snakeCase);
Converter<String, String> snakeToCamel = snakeCase.converterTo(camelCase);
String s0 = "foo_bar_baz";
String c1 = snakeToCamel.convert(s0);
String s1 = camelToSnake.convert(c1);
System.out.println(String.format("%s => %s%n", s0, c1));
@lutzhorn
lutzhorn / FormatDate.java
Created Mar 8, 2019
format Date in Java 8
View FormatDate.java
Date value = new Date():
LocalDate localDate = value.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
String formatedDate = DateTimeFormatter.ISO_LOCAL_DATE.format(localDate);
LocalDateTime localDateTime = value.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
String formatedDateTime = DateTimeFormatter.ISO_LOCAL_DATE.format(localDateTime);
@lutzhorn
lutzhorn / InstantExplore.java
Last active Mar 7, 2019
Convert Instant to LocalDate in Java 11 or Java 8
View InstantExplore.java
package playground;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class InstantExplore {
public static void main(String[] args) {
@lutzhorn
lutzhorn / main.go
Last active Jan 31, 2019
Demonstrate the use of `Rows.NextResultSet`
View main.go
package main
import (
"context"
"database/sql"
"fmt"
"os"
_ "github.com/lib/pq"
)
@lutzhorn
lutzhorn / getPid.py
Created Jan 28, 2019
Function to get the process ID (pid) of a named process
View getPid.py
#!/usr/bin/env python3.5
import os
import subprocess
def getPid(name):
""" Get the process ID (pid) of a named process. """
ps = subprocess.run(["ps", "cax"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if ps.returncode == 0:
@lutzhorn
lutzhorn / certbot-renew.sh
Created Jan 18, 2019
"Action required: Let's Encrypt certificate renewals"
View certbot-renew.sh
# Let's Encrypt sends emails like this:
# Subject: Action required: Let's Encrypt certificate renewals
#
# Hello,
#
# **Action is required to prevent your Let's Encrypt certificate renewals from breaking.**
#
# Your Let’s Encrypt client used ACME TLS-SNI-01 domain validation to issue a certificate in the past 60 days.
#
@lutzhorn
lutzhorn / datetime-with-timezone.sql
Created Jan 17, 2019
Datetime with zone for PostgreSQL: A timestamp should be saved in two columns, one containing the local timestamp, one the name of the zone. Queries on these columns require casts which make usage of an index on an expression advisable.
View datetime-with-timezone.sql
drop table if exists t;
-- Create temporary table with local datetime and time zone.
create temporary table t (
id SERIAL primary key,
"datetime" timestamp without time zone not null default now(),
"zone" varchar(255) not null default 'Europe/Berlin'
);
-- Insert 262,800 generated rows.
@lutzhorn
lutzhorn / interface-slice.go
Created Jan 17, 2019
An Interface Slice in Go: What is the use of `[]Fooer` in Go where `Fooer` is an interface?
View interface-slice.go
package main
import (
"fmt"
)
type Fooer interface {
Foo() string
}
@lutzhorn
lutzhorn / jwt-with-go.go
Created Jan 17, 2019
How I use JWT in Go
View jwt-with-go.go
package main
import (
"errors"
"fmt"
"io/ioutil"
jwt "github.com/dgrijalva/jwt-go"
"github.com/satori/go.uuid"
)
@lutzhorn
lutzhorn / pom.xml
Created Jan 17, 2019
Maven Dependencies for JSON-P
View pom.xml
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.2</version>
</dependency>
You can’t perform that action at this time.