A little Scala “date” utilities class

Here’s some source code for a little Scala “date utilities” class:

import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date

object DateUtils {

    val DATE_FORMAT = "EEE, MMM dd, yyyy h:mm a"

    def getDateAsString(d: Date): String = {
        val dateFormat = new SimpleDateFormat(DATE_FORMAT)
        dateFormat.format(d)
    }

    def convertStringToDate(s: String): Date = {
        val dateFormat = new SimpleDateFormat(DATE_FORMAT)
        dateFormat.parse(s)
    }
    
    def convertDateStringToLong(dateAsString: String): Long = {
        convertStringToDate(dateAsString).getTime
    }
    
    def convertLongToDate(l: Long): Date = new Date(l)
    
    def now(): Date = Calendar.getInstance.getTime

}

If you wanted to see how to do some things with dates in Scala, or otherwise want to start your own date utilities class, I hope this example source code is helpful.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.