我正在添加这个问题,因为我是Java和Android的新手,我搜索了几个小时试图解决这个问题.答案来自相关答案的组合,所以我想我会记录我为其他可能正在努力的人学到的东西.见答案.
对于一些背景知识,我的经验主要是PHP的Web开发和一点Ruby.我唯一的操作系统是Linux(Ubuntu Studio),我(不情愿地)在Android Studio 2.1.2中开发我的第一个Android应用程序.我的Java设置如下:
>java -version
> openjdk version "1.8.0_91"
> OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-3ubuntu1~15.10.1-b14)
> OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)
Run Code Online (Sandbox Code Playgroud) datetime android android-gradle-plugin threetenbp threetenabp
我有两个约会:
String date_1="yyyyMMddHHmmss";
String date_2="yyyyMMddHHmmss";
Run Code Online (Sandbox Code Playgroud)
我想打印差异如:
2d 3h 45m
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?谢谢!
在Java中,给定时间戳,如何将时间部分单独重置为00:00:00,以便时间戳表示该特定日期的午夜?
在T-SQL中,此查询将实现相同的目标,但我不知道如何在Java中执行此操作.
SELECT CAST( FLOOR( CAST(GETDATE() AS FLOAT ) ) AS DATETIME) AS 'DateTimeAtMidnight';
我需要像今天、昨天、2 天前那样打印日期,因为我已经完成了我得到的日期就像:String date1 = "Thu Nov 13 19:01:25 GMT+05:30 2014";
打电话str=get_userTime(date1);
private String get_userTime(String usertime) {
Date d = null;
// String datee = "Thu Nov 13 19:01:25 GMT+05:30 2014";
String datee = usertime;
SimpleDateFormat inputFormat = new SimpleDateFormat(
"EE MMM dd HH:mm:ss zz yyy");
try {
d = inputFormat.parse(datee);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat outputFormat1 = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(outputFormat1.format(d));
op = outputFormat1.format(d);
op=formatToYesterdayOrToday(op);
return op;
}
Run Code Online (Sandbox Code Playgroud)
这是昨天/今天获得的另一个功能,我使用了这个链接
public …Run Code Online (Sandbox Code Playgroud) 我需要计算两个日期之间的天数,我使用下面的代码.问题是它返回我2但实际上它应该返回3因为2016年6月30日到6月27日之间的差异是3.你能帮助它应该包括当前日期以及差异吗?
public static long getNoOfDaysBtwnDates(String expiryDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date expDate = null;
long diff = 0;
long noOfDays = 0;
try {
expDate = formatter.parse(expiryDate);
//logger.info("Expiry Date is " + expDate);
// logger.info(formatter.format(expDate));
Date createdDate = new Date();
diff = expDate.getTime() - createdDate.getTime();
noOfDays = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
long a = TimeUnit.DAYS.toDays(noOfDays);
// logger.info("No of Day after difference are - " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
System.out.println(a);
System.out.println(noOfDays);
} catch (ParseException e) {
e.printStackTrace();
}
return noOfDays;
}
Run Code Online (Sandbox Code Playgroud)
到期日为2016-06-30,当前日期为2016-06-27
我需要在当前日期和未来日期之间取得区别,在android中以天,小时,分钟和秒为单位.如果当前日期和时间是17/05/2012 03:53:00,将来的日期是18/05/2012 04:55:00.
我需要显示差异作为剩余时间=天:1,小时:1和分钟2.
任何形式的帮助将不胜感激.提前谢谢了.
此致,Munazza K.
有谁能帮助我Date以有效的方式计算天数方面的差异?
Date nextCollectionDate = dispenseNormal.getDispensing().getNextCollectionDate();
Date currentDate = new Date();
int daysDiff = currentDate - nextCollectionDate;
Run Code Online (Sandbox Code Playgroud) 我得到今天这样的日期:
final Calendar cal = Calendar.getInstance();
{
mYear = cal.get(Calendar.YEAR);
mMonth = cal.get(Calendar.MONTH);
mDay = cal.get(Calendar.DAY_OF_MONTH);
}
Run Code Online (Sandbox Code Playgroud)
我想计算x天前的日期是什么...有人得到了什么吗?
我试图生成日期x和日期y之间的日期范围,但失败了.我在c#中使用相同的方法,所以我尝试尽可能多地修改它但是没有得到结果.知道我能解决什么吗?
private ArrayList<Date> GetDateRange(Date start, Date end) {
if(start.before(end)) {
return null;
}
int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
ArrayList<Date> listTemp = new ArrayList<Date>();
Date tmpDate = start;
do {
listTemp.add(tmpDate);
tmpDate = tmpDate.getTime() + MILLIS_IN_DAY;
} while (tmpDate.before(end) || tmpDate.equals(end));
return listTemp;
}
Run Code Online (Sandbox Code Playgroud)
说实话,我试图让所有日期从1月1日开始到2012年12月31日结束.如果有更好的方法,请告诉我.谢谢
我想计算两个日期之间的差异.
我有类似的东西:
String deadline = "2015-08-15";
Run Code Online (Sandbox Code Playgroud)
我得到当前的日期:
String timeStamp = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
Run Code Online (Sandbox Code Playgroud)
例如,它将是今天:
2015年11月11日
如何计算这两个日期之间的天数?如果日期过去会发生什么.
android ×6
java ×6
date ×3
datetime ×3
calendar ×1
date-range ×1
days ×1
difference ×1
jodatime ×1
sql ×1
threetenabp ×1
threetenbp ×1
timestamp ×1