Skip to content

Instantly share code, notes, and snippets.

@rupakraj

rupakraj/DateTime.java

Last active Sep 14, 2017
Embed
What would you like to do?
Date Time Class for Java
package util;
import java.util.Calendar;
/**
* Created by rupak on 10/14/15.
* Use: DateTime.getDate().ToDateTime()
* Explore static methods
*/
public class DateTime{
private int _yr=0, _mon=0,_day=0, _hr=0, _min=0, _sec=0;
private String _format="yyyy/mm/dd hh:min:sec";
private String _format_date="yyyy/mm/dd";
private String _format_time="hh:min:dd";
public String ToString(String format){
return format.replace("yyyy", String.valueOf(_yr))
.replace("mm", String.valueOf(_mon))
.replace("dd", String.valueOf(_day))
.replace("hh", String.valueOf(_hr))
.replace("min", String.valueOf(_min))
.replace("sec", String.valueOf(_sec));
}
public String ToString(){
return _format.replace("yyyy", String.valueOf(_yr))
.replace("mm", String.valueOf(_mon))
.replace("dd", String.valueOf(_day))
.replace("hh", String.valueOf(_hr))
.replace("min", String.valueOf(_min))
.replace("sec", String.valueOf(_sec));
}
public String ToDateTime(){
return _format.replace("yyyy", String.valueOf(_yr))
.replace("mm", String.valueOf(_mon))
.replace("dd", String.valueOf(_day))
.replace("hh", String.valueOf(_hr))
.replace("min", String.valueOf(_min))
.replace("sec", String.valueOf(_sec));
}
public String ToDate(String format){
return _format_date.replace("yyyy", String.valueOf(_yr))
.replace("mm", String.valueOf(_mon))
.replace("dd", String.valueOf(_day)) ;
}
public String ToTime(){
return _format_time.replace("hh", String.valueOf(_yr))
.replace("min", String.valueOf(_mon))
.replace("sec", String.valueOf(_day));
}
public static DateTime getDate(){
Calendar cal = Calendar.getInstance();
DateTime dateTime = new DateTime();
dateTime._yr = cal.get(Calendar.YEAR);
dateTime._mon= cal.get(Calendar.MONTH);
dateTime._day = cal.get(Calendar.DAY_OF_MONTH);
dateTime._hr= cal.get(Calendar.HOUR_OF_DAY);
dateTime._min= cal.get(Calendar.MINUTE);
dateTime._sec= cal.get(Calendar.SECOND);
return dateTime;
}
}
@costakatia

This comment has been minimized.

Copy link

@costakatia costakatia commented Sep 14, 2017

how do I use the class Calendar to print odds and even weekdays from a period of time of a years?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.