get Current Weekday - Java java.util

Java examples for java.util:Week

Description

get Current Weekday

Demo Code


//package com.java2s;
import java.text.DateFormat;

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

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getCurrentWeekday());
    }//from   w w w. j  a  va 2 s  .  co m

    private static int weeks = 0;

    public static String getCurrentWeekday() {
        weeks = 0;
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
        Date monday = currentDate.getTime();

        DateFormat df = DateFormat.getDateInstance();
        String preMonday = df.format(monday);
        return preMonday;
    }

    private static int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        // ?......  
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // ?1  
        if (dayOfWeek == 1) {
            return 0;
        } else {
            return 1 - dayOfWeek;
        }
    }
}

Related Tutorials