This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Gregarion calendar causing problem  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
output of incrementing below date 20050530 (30th April)by 1 is 31st April
instead of 1st May 2005 ?? Any explaination ?

import java.text.ParseException;
import java.util.*;
import java.io.*;
public class TestPgm {
public TestPgm() {
}

public static void main(String[] arg) throws ParseException
{
TestPgm t = new TestPgm();
t.extractDate("20050430");
}

public String extractDate(String xml_file_date)//20040728S
{
//String tempDate = latestFileName.substring(9,17).trim();
int yrIndex = Integer.parseInt(xml_file_date.substring(0,4));
int mthIndex = Integer.parseInt(xml_file_date.substring(4,6)) ;
int dayIndex = Integer.parseInt(xml_file_date.substring(6,8));

GregorianCalendar g = new GregorianCalendar(yrIndex,mthIndex,dayIndex);
System.out.println("mthIndex " + mthIndex);
System.out.println("dayIndex " + dayIndex);
System.out.println("yrIndex " + yrIndex);

g.add(Calendar.DATE,1);
String yr = g.get(GregorianCalendar.YEAR)+"";
String mth = g.get(GregorianCalendar.MONTH)+"";
if(mth.length() != 2)
{
mth = "0"+mth;
}
String day = g.get(GregorianCalendar.DATE)+"";
System.out.println("yr " + yr);
System.out.println("mth " + mth);
System.out.println("day " + day);

if(day.length() != 2)
{

day = "0"+day;
}

return yr+""+mth+""+day;
}


}
 
ranger
Sheriff
Posts: 17314
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Deepak"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the Java Documentation on GregorianCalendar month is 0 (zero) based. So, what you thought was April would be May.


GregorianCalendar

public GregorianCalendar(int year,
int month,
int date)

Constructs a GregorianCalendar with the given date set in the default time zone with the default locale.

Parameters:
year - the value used to set the YEAR time field in the calendar.
month - the value used to set the MONTH time field in the calendar. Month value is 0-based. e.g., 0 for January.
date - the value used to set the DATE time field in the calendar.

 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!