Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 22nd, 2007, 01:28 PM
Registered User
 
Join Date: Oct 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Needs to get previous date from the given date

Hi,

I have a requirement to get the previous day for a given date. Please find the code below. It is giving previous day of the current date but i need the previous day of the given date.

Kindly help in this,

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
         GregorianCalendar gc = new GregorianCalendar();
         try{
         gc.setGregorianChange(sdfInput.parse("22-JAN-2007"));
         }catch(ParseException e){
             System.out.println(e.getMessage());
         }
         gc.add(Calendar.DATE,-1);
         Date d = gc.getTime();
    
         System.out.println(sdf.format(d));

Expected output : 21-JAN-2007
Resultant output : current date - 1 i.e suppose today is 22nd April 2007, then it is giving 21-Apr-2007.

Regards
Bala
 
Old April 23rd, 2007, 05:25 AM
Registered User
 
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Balla,
I try the following piece of code:
------------------------------------

           GregorianCalendar gc = new GregorianCalendar();
           gc.setGregorianChange(new Date("22-Jan-2007"));
           Date d = new Date();
           d.setTime((gc.getGregorianChange()).getTime()-86400000);
           System.out.println(d);

------------------------------------

The number 86400000 in code above is 24 hours in milliseconds.

Best,
Anna







Similar Threads
Thread Thread Starter Forum Replies Last Post
Get Previous Date in vb.net kostaskir VB How-To 1 August 31st, 2007 09:21 AM
selecting date and previous dates harpua MySQL 0 April 5th, 2007 08:25 PM
How to get previous date? rupen Javascript How-To 1 June 26th, 2006 10:57 PM
DTS Import ( Date string to Date field) gfowajuh SQL Server 2000 1 September 30th, 2003 06:28 AM
Convert String Date to Date for a SQL Query tdaustin Classic ASP Basics 4 July 7th, 2003 06:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.