2012-06-30 38 views
12

Tôi đang cố gắng tìm nạp ngày 7 ngày trước ngày hôm nay. Tôi đang sử dụng SimpleDateFormat để tìm ngày hôm nay.Nhận ngày trong 7 ngày qua từ hiện tại trong android

SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy"); 

hãy hướng dẫn cho tôi qua

câu trả lời này cập nhật mà tôi thấy hữu ích nhất

SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy"); 
String currentDateandTime = sdf.format(new Date()); 
Date cdate=sdf.parse(currentDateandTime); 
Calendar now2= Calendar.getInstance(); 
now2.add(Calendar.DATE, -7); 
String beforedate=now2.get(Calendar.DATE)+"/"+(now2.get(Calendar.MONTH) + 1)+"/"+now2.get(Calendar.YEAR); 
Date BeforeDate1=sdf.parse(beforedate); 
cdate.compareTo(BeforeDate1); 

Cám ơn các bạn trả lời

+0

Vui lòng chấp nhận câu trả lời nếu bạn đã có giải pháp của bạn;) –

Trả lời

21

Sử dụng java.util.Calendar, đặt thành ngày hôm nay và sau đó trừ đi 7 ngày.

Calendar cal = GregorianCalendar.getInstance(); 
cal.setTime(new Date()); 
cal.add(Calendar.DAY_OF_YEAR, -7); 
Date 7daysBeforeDate = cal.getTime(); 

Edit: Trong Java 8 nó có thể được thực hiện dễ dàng hơn nhiều bằng cách sử dụng các lớp học từ java.time gói:

final LocalDate date = LocalDate.now(); 
final LocalDate dateMinus7Days = date.minusDays(7); 
//Format and display date 
final String formattedDate = dateMinus7Days.format(DateTimeFormatter.ISO_LOCAL_DATE); 
System.out.println(formattedDate); 
+1

có lẽ nên được cal.roll thay vì cal.set? – potatoe

2

Android get date before 7 days (one week)

Date myDate = dateFormat.parse(dateString); 

Và sau đó, hoặc tìm ra bao nhiêu mili giây bạn cần phải trừ:

Date newDate = new Date(myDate.getTime() - 604800000L); // 7 * 24 * 60 * 60 * 1000 

Hoặc sử dụng API được cung cấp bởi lớp java.util.Calendar:

Calendar calendar = Calendar.getInstance(); 
calendar.setTime(myDate); 
calendar.add(Calendar.DAY_OF_YEAR, -7); 
Date newDate = calendar.getTime(); 
Then, if you need to, convert it back to a String: 

và cuối cùng

String date = dateFormat.format(newDate); 
4

Bạn có thể thử này,

import java.util.Calendar; 

public class AddDaysToCurrentDate { 

    public static void main(String[] args) { 

    //create Calendar instance 
    Calendar now = Calendar.getInstance(); 

    System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) 
         + "-" 
         + now.get(Calendar.DATE) 
         + "-" 
         + now.get(Calendar.YEAR)); 

    //add days to current date using Calendar.add method 
    now.add(Calendar.DATE,1); 

    System.out.println("date after one day : " + (now.get(Calendar.MONTH) + 1) 
         + "-" 
         + now.get(Calendar.DATE) 
         + "-" 
         + now.get(Calendar.YEAR)); 


    //substract days from current date using Calendar.add method 
    now = Calendar.getInstance(); 
    now.add(Calendar.DATE, -10); 

    System.out.println("date before 10 days : " + (now.get(Calendar.MONTH) + 1) 
         + "-" 
         + now.get(Calendar.DATE) 
         + "-" 
         + now.get(Calendar.YEAR)); 

    } 
} 

/* 
Typical output would be 
Current date : 12-25-2007 
date after one day : 12-26-2007 
date before 10 days : 12-15-2007 
*/ 
Các vấn đề liên quan