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;
import java.util.Locale;

public class Main {

    public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static int getWeekOfMonth(String time) {
        return getWeekOfMonth(string2Date(time, DEFAULT_PATTERN));
    }

    public static int getWeekOfMonth(String time, String pattern) {
        return getWeekOfMonth(string2Date(time, pattern));
    }

    public static int getWeekOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.WEEK_OF_MONTH);
    }

    public static int getWeekOfMonth(long millis) {
        return getWeekOfMonth(millis2Date(millis));
    }

    public static Date string2Date(String time) {
        return string2Date(time, DEFAULT_PATTERN);
    }

    public static Date string2Date(String time, String pattern) {
        return new Date(string2Millis(time, pattern));
    }

    public static Date millis2Date(long millis) {
        return new Date(millis);
    }

    public static long string2Millis(String time) {
        return string2Millis(time, DEFAULT_PATTERN);
    }

    public static long string2Millis(String time, String pattern) {
        try {
            return new SimpleDateFormat(pattern, Locale.getDefault()).parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }
}