Scala: How to get the current month as a number or string

Table of Contents1 - Get the current month as an integer number2 - How to get the current month as an abbreviated string3 - How to get the full month name4 - A note about SimpleDateFormat and Locale5 - Summary

Scala FAQ: How do I get the current month as an integer or as a string in Scala?

A Scala method to replace a few “bad” characters

Here’s a little Scala method I wrote to replace some “bad” characters that won’t print properly on my Radio Pi display:

def replaceBadCharacters(s: String): String = {
    s.replaceAll("“", "\"")
     .replaceAll("”", "\"")
     .replaceAll("‘", "\"")
     .replaceAll("’", "\"")

There are other ways to solve this problem, but I threw this together as a quick patch until I can figure out why the Phosphor screen saver on the Raspberry Pi won’t print those characters that I’m replacing.

Scala “string to date” and “date to string” methods

Here are a couple of “string to date” and “date to string” methods. They’re written in Scala, but are easily converted to Java. They only use the Java Date and SimpleDateFormat classes:

Scala type aliases (syntax, examples)

In Scala you can declare a type alias. Typically you do this to create a simple alias for a more complex type.

Using a type alias to simplify a complex type

For example, on this page I note a good example where someone on StackOverflow first defined a type alias named Row, and then created a second type alias named Matrix as a list of rows:

A collection of 100+ Scala String examples

This page contains a collection of over 100 Scala String examples, including strings functions, format specifiers, and more. I don’t provide too many details about how things work in these examples; this is mostly just a collection of examples that can be used as a reference page or cheat sheet. (I do show the output of most examples.)

First, here are some basic uses of the Scala String class to help get us warmed up:

A ‘printf’ format reference page (cheat sheet)

Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it here.

A cool thing about the printf formatting syntax is that the specifiers you can use are very similar, if not identical, between several different languages, including C, C++, Java, Perl, Ruby, and others, so your knowledge is reusable, which is a good thing.

Android: How to convert a list of strings or objects to a single string alvin August 2, 2017 - 4:39pm

Android FAQ: How do I convert a list of strings (or a list of objects) to a single, combined string?

In Android, if you want to convert a list of strings to a String, I just saw this approach:

How to write “If string starts with” in Drupal 8 Twig templates

If you ever need to write a “string starts with” comparison in Drupal 8 Twig templates, I just used this approach in a node.html.twig template file and I can confirm that it works:

{% if uri starts with '/foo' %}

More accurately, what I did was to first get the URI for the current Drupal node, and then I perform that test: