Skip to content

Instantly share code, notes, and snippets.

@andy722
Created August 29, 2016 08:21
Show Gist options
  • Select an option

  • Save andy722/bcfffa40a61911b346a0701a3cfdbaf1 to your computer and use it in GitHub Desktop.

Select an option

Save andy722/bcfffa40a61911b346a0701a3cfdbaf1 to your computer and use it in GitHub Desktop.
In given CSV file, converts dates from _full_ format to short form
import java.text.DateFormat
import java.util.regex.Pattern
//
// In given CSV file, converts dates formatted like
//
// Monday, August 1, 2016 8:39:28 PM +07:00
//
// to dates like
//
// 01.08.2016
//
final IN = new File('/Users/andy/Downloads/issues-4.csv')
final OUT = new File("/tmp/out.csv")
final formatter = DateFormat.getDateTimeInstance(
DateFormat.FULL,
DateFormat.MEDIUM,
Locale.US)
final p = Pattern.compile('"([^"]+?07:00)"')
final targetFormat = 'dd.MM.yyyy'
IN.eachLine { l ->
final m = p.matcher l
while (m.find()) {
final inDate = m.group 1
final outDate = formatter.parse(inDate).format(targetFormat)
l = l.replace inDate, outDate
}
OUT.append(l + "\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment