(dry) How to get the start time and the last day of the week in Java based on a time of the year. And the set start time is Week 6. End Time is 5

Source: Internet
Author: User
Tags date1



I test, useful, relatively strong applicability, directly on the code to speak.


package com.helloBike.data;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

public class Data {

public static void main (String [] args) throws ParseException {
// LocalDate localDate = LocalDate.parse ("2017-10-07");
// LocalDate nextSunday = localDate.with (TemporalAdjusters.next (DayOfWeek.SUNDAY));
// System.out.println (localDate);
// System.out.println (nextSunday);
Ranch
// a (20171004);
// System.out.println ("****************************************" );
getFirstDay ("2017-10-17");
System.out.println ("****************************************");
getLastDay1 ("2017-10-17");
SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd"); // Set the date format
String a = "2017-10-17"; // new Date () is to get the current system time
Date b = df.parse (a);
System.out.println ("Date a =" + b);
if (true == identyWeek (b)) {
System.out.println ("1");
} else {
System.out.println ("0");
}
// stampToDate ("20171004");
// LocalDate c = LocalDate.now ();
// System.out.println ("LocalDate =" + c);
// localToDate (c);
// System.out.println ("localToDate (c) =" + localToDate (c));
// System.out.println ("****************************************" );

}
Ranch
        / **
* Obtain the current time and judge with the incoming time. When the last day of the incoming time is less than the last day of the day, it means that the statement will not be executed if the week is not over.
* @param data
* @return
* @throws ParseException
* /
public static boolean identyWeek (Date data) throws ParseException {
// Get the last day of the incoming time
SimpleDateFormat date1 = new SimpleDateFormat ("yyyy-MM-dd");
System.out.println ("identyWeekdate1 =" + date1);
Ranch
String date2 = date1.format (data);
System.out.println ("identyWeek date2 =" + date2);
Ranch
System.out.println ("getLastDay date2");
String lastTime = getLastDay1 (date2);
Ranch
System.out.println ("identyWeek lastTime =" + lastTime);
// Get the current time
Date date = new Date (); // Create a time object, get the current time
System.out.println ("current time date" + date);
Ranch
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); // Set the time display format
String str = sdf.format (date); // Format the current time to the required type
System.out.println ("identyWeek str =" + str);
Ranch
System.out.println ("getLastDay1");
String nowLastTime = getLastDay1 (str);
System.out.println ("nowLastTime =" + nowLastTime);
// When the last day of the current time and the last day of the start time
int res = lastTime.compareTo (nowLastTime);
        if (res <0)
            return true;
        else
return false;
}
Ranch
        / **
* Pass in a date of the string type and return the last day of the week for that date (Where 5 is set as the last day)
* getLastDay
* @param x
* @throws ParseException
* /
public static String getLastDay1 (String x) throws ParseException {
Ranch
Date InputDate = new SimpleDateFormat ("yyyy-MM-dd"). Parse (String.valueOf (x));
// Create a calendar object cDate
Calendar cDate = Calendar.getInstance ();
// Call the method of the calendar object to get the value of the first day of the week
cDate.setFirstDayOfWeek (Calendar.MONDAY);
// Pass the time to the calendar object
cDate.setTime (InputDate);
// Create a calendar object firstDate
Calendar lastDate = Calendar.getInstance ();
// Set the date parameter of the first day of the week, here set to monday Monday, Monday to Sunday
lastDate.setFirstDayOfWeek (Calendar.MONDAY);
// Set the calendar time, the current time is the time entered
lastDate.setTime (InputDate);
// Set the obtained year relative to the first year ,. Useless
if (cDate.get (Calendar.WEEK_OF_YEAR) == 1 && cDate.get (Calendar.MONTH) == 11) {
lastDate.set (Calendar.YEAR, cDate.get (Calendar.YEAR) +1);
}
// get this is the day of the week relative to the week, the return value of week 2 is 3
int week = cDate.get (Calendar.DAY_OF_WEEK) -1;
System.out.println ("week =" + week);
// Judge whether this week is Sunday, Sunday, and if so, calculate the start time of the next week, and then calculate the time of Saturday,
if (week == 0 || week == 6) {
System.out.println ("on Sunday or Saturday");
System.out.println ("x =" + x);
// Get the week number of the year
int typeNum = cDate.get (Calendar.WEEK_OF_YEAR) +1;
// Set the week number of the year
lastDate.set (Calendar.WEEK_OF_YEAR, typeNum);
// Set the number of days this week, this week is positioned as SATURDAY
lastDate.set (Calendar.DAY_OF_WEEK, Calendar.SATURDAY + 6);
// Set to set this Sunday to the first day
//firstDate.setFirstDayOfWeek(Calendar.FRIDAY);
// The start date of the week
String lastTime = new SimpleDateFormat ("yyyy-MM-dd"). Format (lastDate.getTime ());
return lastTime;
} else {
// if (week == 1 || week == 2 || week == 3 || week == 4 || week == 5)
Ranch
System.out.println ("No longer Saturday or Sunday");
System.out.println ("x =" + x);
// When this week is not Saturday and Sunday
System.out.println ("When blocked on Monday or Saturday");
// Get week number of this year -1
int typeNum = cDate.get (Calendar.WEEK_OF_YEAR);
System.out.println ("Week of the week:" + typeNum);
// Set the week number of the year
lastDate.set (Calendar.WEEK_OF_YEAR, typeNum);
// Set the number of days this week, this week is positioned as SATURDAY
lastDate.set (Calendar.DAY_OF_WEEK, Calendar.SATURDAY + 6);
// Set to set this Sunday to the first day
//firstDate.setFirstDayOfWeek(Calendar.FRIDAY);
// The start date of the week
String lastTime = new SimpleDateFormat ("yyyy-MM-dd"). Format (lastDate.getTime ());
return lastTime;
Ranch
}
}
/ **
            * Get the system time of the day, convert localdate format to date format date
            *
            * /
public static Date localToDate (LocalDate a) throws ParseException {
// Set the conversion format. Time offset id
ZoneId zone = ZoneId.systemDefault ();
// Use ZonedDateTime to convert LocalDate to Instant.
Instant instant = a.atStartOfDay (). AtZone (zone) .toInstant ();
// Get the Date instance from the Instant object using the from () method
java.util.Date date = Date.from (instant);
// Distinguish data according to a certain format.
String b = new SimpleDateFormat ("yyyy-MM-dd"). Format (date.getTime ());
System.out.println ("b =" + b);
Date c = new SimpleDateFormat ("yyyy-MM-dd"). Parse (b);
System.out.println ("c =" + c);
return c;
}
Ranch
Ranch
public static String stampToDate (String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyyMMdd");
        long lt = new Long (s);
        Date date = new Date (lt);
        res = simpleDateFormat.format (date);
        System.out.println ("res =" + res);
        return res;
}
Ranch
Ranch
Ranch
/ **
* Pass a string type parameter to return the last day of the week (here the last day is set to week 5)
* get last day
* /
public static void getLastDay (String x) throws ParseException {
Date InputDate = new SimpleDateFormat ("yyyy-MM-dd"). Parse (String.valueOf (x));
// Create a calendar object cDate
Calendar cDate = Calendar.getInstance ();
// Call the method of the calendar object to get the value of the first day of the week
cDate.setFirstDayOfWeek (Calendar.MONDAY);
// Pass the time to the calendar object
cDate.setTime (InputDate);
// Create a calendar object firstDate
Calendar lastDate = Calendar.getInstance ();
// Set the date parameter of the first day of the week, here set to monday Monday, Monday to Sunday
lastDate.setFirstDayOfWeek (Calendar.MONDAY);
// Set the calendar time, the current time is the time entered
lastDate.setTime (InputDate);
// Set the obtained year relative to the first year ,. Useless
if (cDate.get (Calendar.WEEK_OF_YEAR) == 1 && cDate.get (Calendar.MONTH) == 11) {
lastDate.set (Calendar.YEAR, cDate.get (Calendar.YEAR) +1);
}
// get this is the day of the week relative to the week, the return value of week 2 is 3
int week = cDate.get (Calendar.DAY_OF_WEEK);
System.out.println ("week =" + week);
// Judge whether this week is Sunday, Sunday, and if so, calculate the start time of the next week, and then calculate the time of Saturday,
if (week == 0 || week == 6) {
System.out.println ("on Monday or Saturday");
System.out.println ("x =" + x);
// Get the week number of the year
int typeNum = cDate.get (Calendar.WEEK_OF_YEAR) +1;
// Set the week number of the year
lastDate.set (Calendar.WEEK_OF_YEAR, typeNum);
// Set the number of days this week, this week is positioned as SATURDAY
lastDate.set (Calendar.DAY_OF_WEEK, Calendar.SATURDAY + 6);
// Set to set this Sunday to the first day
//firstDate.setFirstDayOfWeek(Calendar.FRIDAY);
// The start date of the week
String firstTime = new SimpleDateFormat ("yyyy-MM-dd"). Format (lastDate.getTime ());
Ranch
System.out.println ("firstTime:" + firstTime);
} else {
System.out.println ("No longer Saturday or Sunday");
System.out.println ("x =" + x);
// When this week is not Saturday and Sunday
System.out.println ("When blocked on Monday or Saturday");
// Get week number of this year -1
int typeNum = cDate.get (Calendar.WEEK_OF_YEAR);
System.out.println ("Week of the week:" + typeNum);
// Set the week number of the year
lastDate.set (Calendar.WEEK_OF_YEAR, typeNum);
// Set the number of days this week, this week is positioned as SATURDAY
lastDate.set (Calendar.DAY_OF_WEEK, Calendar.SATURDAY + 6);
// Set to set this Sunday to the first day
//firstDate.setFirstDayOfWeek(Calendar.FRIDAY);
// The start date of the week
String lastTime = new SimpleDateFormat ("yyyy-MM-dd"). Format (lastDate.getTime ());
System.out.println ("firstTime:" + lastTime);
}
}
Ranch
/ **
*
* Pass in a time parameter and return the Saturday of the date
* @param x
* @throws ParseException
* /
public static void getFirstDay (String x) throws ParseException {
Date InputDate = new SimpleDateFormat ("yyyy-MM-dd"). Parse (String.valueOf (x));
// Create a calendar object cDate
Calendar cDate = Calendar.getInstance ();
// Call the method of the calendar object to get the value of the first day of the week
cDate.setFirstDayOfWeek (Calendar.MONDAY);
// Pass the time to the calendar object
cDate.setTime (InputDate);
// Create a calendar object firstDate
Calendar firstDate = Calendar.getInstance ();
// Set the date parameter of the first day of the week, here set to monday Monday, Monday to Sunday
firstDate.setFirstDayOfWeek (Calendar.MONDAY);
// Set the calendar time, the current time is the time entered
firstDate.setTime (InputDate);
// Set the obtained year relative to the first year ,. Useless
if (cDate.get (Calendar.WEEK_OF_YEAR) == 1 && cDate.get (Calendar.MONTH) == 11) {
firstDate.set (Calendar.YEAR, cDate.get (Calendar.YEAR) +1);
}
// get this is the day of the week relative to the week, the return value of week 2 is 3
int week = cDate.get (Calendar.DAY_OF_WEEK) -1;
System.out.println ("week =" + week);
// Judge whether this week is Sunday, Sunday, and if so, calculate the start time of the next week, and then calculate the time of Saturday,
if (week == 0 || week == 6) {
System.out.println ("When blocked on Monday or Saturday");
System.out.println ("x =" + x);
// Get the week number of the year
int typeNum = cDate.get (Calendar.WEEK_OF_YEAR);
// Set the week number of the year
firstDate.set (Calendar.WEEK_OF_YEAR, typeNum);
// Set the number of days this week, this week is positioned as SATURDAY
firstDate.set (Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
// Set to set this Sunday to the first day
//firstDate.setFirstDayOfWeek(Calendar.FRIDAY);
// The start date of the week
String firstTime = new SimpleDateFormat ("yyyy-MM-dd"). Format (firstDate.getTime ());
Ranch
System.out.println ("firstTime:" + firstTime);
} else {
System.out.println ("No longer Saturday or Sunday");
System.out.println ("x =" + x);
// When this week is not Saturday and Sunday
System.out.println ("When blocked on Monday or Saturday");
// Get week number of this year -1
int typeNum = cDate.get (Calendar.WEEK_OF_YEAR) -1;
System.out.println ("Week of the week:" + typeNum);
// Set the week number of the year
firstDate.set (Calendar.WEEK_OF_YEAR, typeNum);
// Set the number of days this week, this week is positioned as SATURDAY
firstDate.set (Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
// Set to set this Sunday to the first day
//firstDate.setFirstDayOfWeek(Calendar.FRIDAY);
// The start date of the week
String firstTime = new SimpleDateFormat ("yyyy-MM-dd"). Format (firstDate.getTime ());
System.out.println ("firstTime:" + firstTime);
}
}

Ranch
/ **
* Pass in a time parameter of type int, return the day of the week, the day of the week, the start time of the week, and the end time of the week (Monday is the start time and Sunday is the end time)
      ** /
public static void a (int x) throws ParseException {
// Create a new date type,
Date InputDate = new SimpleDateFormat ("  





(dry) How to get the start time and the last day of the week in Java based on a time of the year. And the set start time is Week 6. End Time is 5


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.