Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2013 Vishwa Patel
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License in the 'assets' directory of this 
 * application or at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Calendar;

public class Main {
    public static String getFormattedCommentCreatedAt(String date) {
        String[] arr = date.split("\\s");
        Calendar calendar = Calendar.getInstance();
        StringBuilder stringBuilder = new StringBuilder();
        String hours = arr[3].split(":")[0];
        String minutes = arr[3].split(":")[1];
        if (Integer.parseInt(arr[5]) != calendar.get(Calendar.YEAR)) {
            stringBuilder.append(arr[1]).append(' ').append(arr[2]).append(',').append(' ').append(arr[5]);
        } else {
            stringBuilder.append(arr[1]).append(' ').append(arr[2]).append(" at ");
        }
        if (Integer.parseInt(hours) < 12) {
            stringBuilder.append(" at ").append(hours).append(':').append(minutes);
            stringBuilder.append(" AM ");
        } else {
            int hoursDigits = Integer.parseInt(hours);
            hoursDigits = hoursDigits - 12;
            hours = Integer.toString(hoursDigits);
            stringBuilder.append(" at ").append(hours).append(':').append(minutes);
            stringBuilder.append(" PM ");
        }
        return stringBuilder.toString();
    }
}