For loop not looping through all data - java

The For Loop is not looping in my codes through all my data. I've read through it thoroughly and still couldn't find any error.
Hope its not some stupid mistake.
Here's a snippet of my for loop codes:
String convertedDuration= "";
String timeConverted = convertedDuration;
for (int i = 0; i < submissionTime.length; i ++)
{
String strDate = submissionTime[i];
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date getDate = sdf.parse(strDate);
getDate.getTime();
convertedDuration = timeConverted + (getDate.getTime());
}
System.out.println("convertedDuration : "+ convertedDuration);
thanks for any help in advance :)

print inside your loop not outside .your are only print last one.move print line to loop.
for (int i = 0; i < submissionTime.length; i ++)
{
String strDate = submissionTime[i];
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date getDate = sdf.parse(strDate);
getDate.getTime();
convertedDuration = timeConverted + (getDate.getTime());
System.out.println("convertedDuration : "+ convertedDuration);
}

Related

How to set text by date and increment loop android?

I want to set text by date and incrementing loop, and when the day changes looping start from the beginning.
Example
1. day 1 =
a. nameFile 110920190001
b. nameFile 110920190002, etc.
2. day 2 =
a. nameFile 120920190001
b. nameFile 120920190002, etc.
Code
Date documentsDate = Calendar.getInstance().getTime();
SimpleDateFormat documentDates = new SimpleDateFormat("ddMMyy");
String setTitleDocument = documentDates.format(documentsDate);
for(int i = 1; i <= 1000; i++) {
String countDocument = String.format("%04d", i);
textNameDocument.setText("Document " + setTitleDocument + countDocument);
}
Just put the Date Initialization in the for loop for it to always take the new Instance of the date.
public static void replace(String s) {
for (int i = 1; i <= 1000; i++) {
Date documentsDate = Calendar.getInstance().getTime();
SimpleDateFormat documentDates = new SimpleDateFormat("ddMMyy");
String setTitleDocument = documentDates.format(documentsDate);
String countDocument = String.format("%04d", i);
textNameDocument.setText("Document " + setTitleDocument + countDocument);
}
}

How to change json string of date and format it to only show the day without, year, month, or time

I have a JSONObject and an ArrayList from where i get the data, the data date string i get is yyyy/MM/dd 00:00:00, i only want to show the day of the month 1-31, how would i format that string to only show the day ?
I've tried to use SimpleDateFormat, but without any luck, maybe since I'm doing this inside a for loop ?
public void onResponse(String response) {
try {
//getting the whole json object from the response
JSONObject obj = new JSONObject(response);
ArrayList<ListModel> ListModelArrayList = new ArrayList<>();
JSONArray dataArray = obj.getJSONArray("events");
for (int i = 0; i < dataArray.length(); i++) {
ListModel List = new ListModel();
JSONObject dataobj = dataArray.getJSONObject(i);
List.setId(dataobj.getString("id"));
List.setInit_date(dataobj.getString("init_date"));
List.setEnd_date(dataobj.getString("end_date"));
List.setTitle(dataobj.getString("title"));
List.setDescription(dataobj.getString("description"));
List.setColor_code(dataobj.getString("color_code"));
List.setAll_day(dataobj.getString("all_day"));
ListModelArrayList.add(List);
}
for (int j = 0; j < ListModelArrayList.size(); j++) {
textViewDate.setText(textViewDate.getText() +
ListModelArrayList.get(j).getInit_Date() + "\n");
textViewEvent.setText(textViewEvent.getText() +
ListModelArrayList.get(j).getTitle() + "\n");
}
right now i am getting this format 2019-05-17 00:00:00, i want to display 17 only
You can alter SimpleDateFormat as you suggested and instead of putting the date directly in the for loop here :
textViewDate.setText(textViewDate.getText() +
ListModelArrayList.get(j).getInit_Date() + "\n");
You will do the following inside your loop :
String s = ListModelArrayList.get(j).getInit_Date();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
Date date = null;
try {
date = fmt.parse(s);
SimpleDateFormat fmtOut = new SimpleDateFormat("dd", Locale.ENGLISH);
textViewDate.setText(textViewDate.getText() +
fmtOut.format(date) + "\n");
} catch (ParseException e) {
textViewDate.setText(textViewDate.getText() + "\n");
e.printStackTrace();
}
Which allows you to format the date according to the pattern you wish in this line :
SimpleDateFormat fmtOut = new SimpleDateFormat("dd", Locale.ENGLISH);
references for patterns
Oracle Documentation
SimpleDateFormat Patterns

How to get hours and minutes from a String variable ?

I have a String timme = "13:10". I was wondering how would I best go about getting the hours and minutes and converting them into integers. i.e. int hours = 13 , int minute = 10. I know a for-loop wouldn't be the most efficient way, is there anything simpler?
Try this way, by split() method,
String timme = "13:10";
String[] time = timme.split ( ":" );
int hour = Integer.parseInt ( time[0].trim() );
int min = Integer.parseInt ( time[1].trim() );
Better use Date and DateFormat:
String time = "13:10";
DateFormat sdf = new SimpleDateFormat("HH:mm"); // or "hh:mm" for 12 hour format
Date date = sdf.parse(time);
date.getHours(); // int
date.getMinutes(); // int
You can try this...
String time = "13:10";
SimpleDateFormat formatter = = new SimpleDateFormat("hh:mm");
Date dTime = formatter.parse(time);
int hour = dTime.getHours();
int minute = dTime.getMinutes();
You could use Calendar object of java instead of Date object,
SimpleDateFormat dateFormatter = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
try {
Calendar c = Calendar.getInstance();
c.setTime(dateFormatter.parse("07:30"));
String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
String mintue = String.valueOf(c.get(Calendar.MINUTE));
System.out.println("Hour: " + hour);
System.out.println("Minute: " + mintue);
} catch (ParseException e) {
e.printStackTrace();
}
JAVA 8
LocalTime localTime = LocalTime.parse("13:10", DateTimeFormatter.ofPattern("HH:mm"));
int hour = localTime.getHour();
int minute = localTime.getMinute();
System.out.println("Hours : "+hour +" Minutes : "+minute);

Convert the string "8:00" into the minutes (integer value)

I'm reading the data from CSV file. One of the fields is the time in the format H:mm, i.e. "8:00". How to convert this string value into the minutes (integer value), i.e. 8:00 = 8*60 = 480 minutes?
String csvFilename = "test.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
String[] row = null;
csvReader.readNext(); // to skip the headers
int i = 0;
while((row = csvReader.readNext()) != null) {
int open = Integer.parseInt(row[0]);
}
csvReader.close();
You can use java.text.SimpleDateFormat to convert String to Date. And then java.util.Calendar to extract hours and minutes.
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date date = sdf.parse("8:00");
cal.setTime(date);
int mins = cal.get(Calendar.HOUR)*60 + cal.get(Calendar.MINUTE);
Try something like this
String str = "8:10";
int minutes=0;
String[] arr= str.split(":");
if(arr.length==2){
minutes=Integer.parseInt(arr[0])*60+Integer.parseInt(arr[1]);
}
System.out.println(minutes);
Write something like this to convert into int
public int convertToMin(String hrmin) {
String[] tokens = hrmin.split(":");
int minutes = 0;
for (int i = tokens.length; i > 0; i--) {
int value = Integer.parseInt(tokens[i - 1]);
if (i == 1) {
minutes += 60 * value;
}
else {
minutes += value;
}
}
return minutes;
}
Try this
String str = "8:20";
int ans = (Integer.parseInt(str.split(":")[0])* 60)+Integer.parseInt(str.split(":")[1]);
System.out.println("Answer = "+ans);

Not able to add 15 minute to 00:00

I have written a function for adding time in a 24 hour format as given below
for (int i = 0; i < cursor.getCount(); i++) {
String RevisedTime="00:00";
// get hour and minute from time string
StringTokenizer st1 = new StringTokenizer(RevisedTime, ":");
int j = 0;
int[] val = new int[st1.countTokens()];
// iterate through tokens
while (st1.hasMoreTokens()) {
val[j] = Integer.parseInt(st1.nextToken());
j++;
}
// call time add method with current hour, minute and minutesToAdd,
// return added time as a string
String date = addTime(val[0], val[1], 15);
}
public String addTime(int hour, int minute, int minutesToAdd) {
Calendar calendar = new GregorianCalendar(1990, 1, 1, hour, minute);
calendar.add(Calendar.MINUTE, minutesToAdd);
SimpleDateFormat sdf = new SimpleDateFormat("kk:mm");
String date = sdf.format(calendar.getTime());
return date;
}
The problem is that while adding 15 minutes to 00:00 I am getting the output as 12.15....
I need to get it as 00:15......Pleas help me.....
You have to use 0-23 hour format, not 1-24.
Instead of SimpleDateFormat sdf = new SimpleDateFormat("kk:mm");
use SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
You just have to change a bit and it would work fine.
Replace
SimpleDateFormat sdf = new SimpleDateFormat("kk:mm");
By
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

Categories

Resources