Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static String formatDate(String date, int minutes) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = null;
        try {
            d = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - minutes);
        return sdf.format(calendar.getTime());
    }

    public static String format(String format, Object targetObc) {
        String target = String.valueOf(targetObc);
        if (format != null && !format.equals("")) {
            if (format.contains("#")) {
                String head = format.substring(0, format.indexOf("#"));
                String body = format.substring(format.indexOf("#"));
                //String tail=format.substring(format.lastIndexOf("#")+1);
                StringBuilder result = new StringBuilder(head);
                int j = 0;
                for (int i = 0; i < target.length(); i++) {
                    if (j > body.length() - 1) {
                        j = 0;
                    }
                    String s = String.valueOf(body.charAt(j));
                    if ("#".equals(s)) {
                        result.append(String.valueOf(target.charAt(i)));
                    } else {
                        result.append(String.valueOf(body.charAt(j)));
                        //result.append(String.valueOf(target.charAt(i)));
                        i--;
                    }
                    j++;
                }
                return result.toString();
            } else {
                return target;
            }
        } else {
            return target;
        }
    }
}