-1

One of the fields of my json is a String which contains time in the following format: 2017-04-04T20:22:33 or even 2017-04-07T12:21:10.0355277+00:00. My task is to convert it to a more simple representation like this: "Apr-4 20:22". I was thinking about about creating a SimpleDateFormat, however it doesn't seem to work with such DateTime format.

Is there any effective way to handle this task?

SpKiwi
  • 202
  • 3
  • 12
  • SimpleDateFormat will work, if you find the problem with it, can you post what have u done? – nafas Apr 07 '17 at 12:27
  • The thing is, SimpleDateFormat needs a String-pattern as one of its parameters. Ok, I pass it a "MMM d H:mm" value to match the result I need. However when I use the "format" method later, it needs me to pass a date into it, but I don't have any clues on how to get the date from the Strings I've described. – SpKiwi Apr 07 '17 at 12:30
  • http://stackoverflow.com/questions/2375222/java-simpledateformat-for-time-zone-with-a-colon-separator where are all possible formats you want – Yu Jiaao Apr 07 '17 at 12:31
  • Please, post your code. – riccardo.cardin Apr 07 '17 at 12:32
  • You have to use `parse` to convert a `String` into a `Date`, not `format` method. Please, have a look to https://docs.oracle.com/javase/8/docs/api/java/text/DateFormat.html#parse-java.lang.String-. Consider to switch to Java 8 Date-Time framework is possible. – riccardo.cardin Apr 07 '17 at 12:37
  • Tip: Search Stack Overflow for ISO 8601, OffsetDateTime, ZoneOffset, and DateTimeFormatter. Your Question and these Answers are using troublesome old date-time classes now supplanted by the java.time classes. `OffsetDateTime.parse( "2017-04-07T12:21:10.0355277+00:00" )` – Basil Bourque Apr 07 '17 at 17:32

4 Answers4

3

I think you're confused between parse and format:

public String convert(String inputStr, String inputFormat, String outputFormat){

    Date date = new SimpleDateFormat(inputFormat).parse( inputStr );
    String outputStr = new SimpleDateFormat(outputFormat).format( date );

    return outputStr;
}

Use it like this:

convert("2017-04-04T20:22:33", "yyyy-MM-dd'T'HH:mm:ss", "d-MMM HH:mm");

Since you don't need the extraneous portions of the String you can remove it entirely before passing to the above method.

dev8080
  • 3,950
  • 1
  • 12
  • 18
2

Try this

 String date="2017-04-04T20:22:33";
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
 SimpleDateFormat sdf1 = new SimpleDateFormat("MMM-dd HH:mm");
 try{
    Date parseDate = sdf.parse(date);
    String output = sdf1.format(parseDate);
 }catch(Exception e) {

 }
Ravi Theja
  • 3,371
  • 1
  • 22
  • 34
  • FYI: The troublesome old date-time classes such as `Date` and `Calendar` are now legacy, supplanted by the java.time classes. – Basil Bourque Apr 07 '17 at 17:24
0

You can use DateUtils

Date truncatedDate = DateUtils.truncate(new Date(), Calendar.DATE);

or you can try localDateTime

LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES)
neverwinter
  • 810
  • 2
  • 15
  • 42
0

I sometimes use a pattern:

private static final Pattern XSD_DATETIME_PATTERN = Pattern.compile(
        "([+-]?[0-9]{4})-(1[0-2]|0[1-9])-([0-9]{2})"
        + "[Tt]([0-9]{2}):([0-9]{2}):([0-9]{2})(?:[.]([0-9]+))?"
        + "(?:([Zz])|([+-][0-9]{2}:[0-9]{2}))?");


public static Date parseXsdDateTime(String s) {
    Matcher matcher = XSD_DATETIME_PATTERN.matcher(s);
    if (!matcher.matches()) {
        throw new IllegalArgumentException(
                "Unparseable date: \"" + s + "\"");
    }
    // Determine timezone
    TimeZone tz;
    if (matcher.group(8) != null) {
        tz = TimeZone.getTimeZone("GMT");
    } else if (matcher.group(9) != null) {
        tz = TimeZone.getTimeZone("GMT" + matcher.group(9));
    } else {
        tz = TimeZone.getDefault();
    }
    Calendar cal = Calendar.getInstance(tz);
    cal.set(Calendar.YEAR, Integer.parseInt(matcher.group(1)));
    cal.set(Calendar.MONTH, Integer.parseInt(matcher.group(2))-1);
    cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(matcher.group(3)));
    cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(matcher.group(4)));
    cal.set(Calendar.MINUTE, Integer.parseInt(matcher.group(5)));
    cal.set(Calendar.SECOND, Integer.parseInt(matcher.group(6)));
    String millis = matcher.group(7);
    if (millis == null) {
        cal.set(Calendar.MILLISECOND, 0);
    } else {
        if (millis.length() > 3) {
            millis = millis.substring(0, 3);
        } else {
            while (millis.length() < 3) {
                millis += "0";
            }
        }
        cal.set(Calendar.MILLISECOND, Integer.parseInt(millis));
    }
    return cal.getTime();
}
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24