Advanced date formatting with skript-mirror

Skript doesn't offer much support for date formatting. Fortunately, Java provides advanced support for date formatting through String#format. Let's access it using skript-mirror!

This tutorial is friendly to users playing around in the server console. Because none of the following examples require a player, you can follow along using Skript's effect commands!

First, import Java's String class. This will allow you to alias the class to a variable instead of typing out the full name. This only has to be done once (until you clear or overwrite the variable), so it can be done once through an effect command or in an "on script load" event.
import "java.lang.String"


Next, let's prepare the arguments we'll pass to String#format in a list variable.
set {format::*} to "The world will end on %%tc" and now.getTimestamp()

Notice, we have to use now.getTimeStamp() to convert Skript's dates into timestamps that String#format can use.

Let's print it out!
message "%{String}.format({format::*})%"

You should get something similar to "The world will end on Fri Jun 23 14:12:27 PDT 2017".

What if we want to use another format? Let's use another preset format.
set {format::*} to "The world will end on %%tD" and now.getTimestamp()

Print it out using the same message code, and you should get something like "The world will end on 06/23/17".

That's great, but what if we want to use our own date format? What if we want to pick what information we want to display? Let's try the same thing, but only display the names of the month and day.
set {format::*} to "The world will end in %%tB on a %%tA" and now.getTimestamp()

Use the same code to print a message... and you should get an error. That's because whenever we use a format specifier (that's the %%tX part), it uses the next argument in the list. String#format is expecting to use a second argument, but we only provided one. We can change that by indexing our format specifiers.
set {format::*} to "The world will end in %%1$tB on a %%1$tA" and now.getTimestamp()

Now you should get something like "The world will end in June on a Friday"!

There's so much more you can do with String#format that I don't have time to cover here, but you can check out the huge list of supported specifiers here.